last day (15 days later) » 

13:59
0
Q: CRUD interface on JSF page is not updating to reflect the adding of new entities

ChuckyThis is my first time using JSF, Hibernate, and Primefaces all at once. My CRUD has a submit button on a new category dialog. The button is coded like so: <p:commandButton value="Submit" update=":form:categoryTable" actionListener="#{categoryController.addCatego...

for the submit dialog that doesn't disappear, you can use oncomplete="widgetVar.hide();" in your commandButton.
Thank you, Eric C. The box is now hiding. Pankaj, I've changed actionListener to action and it has no effect; my added categories are still not appearing in the dataTable.
@Chucky Are you maintaining a list of objects in your managed bean for your datatable? If that is the case you have to add the newly created object in that list.
Almost there, Eric C! The list is now showing entities, but every time a new one is added, it replaces every entity there. So when adding Geography, then History, Geography turned to History so that there are now 2 history records. I added a categories.add line into my managedBean, and now this happens?
It is quite unclear what you're trying to do. You should show the code of your managed bean. What I can say is that if you have a list to populate your datatable, when creating a new record (Category?), you should add the newly created record to the list by a simple List.add(record). Updating the table should then show all records in the list.
13:59
That is exactly what I have done (see edit) but for some reason every previous entry gets changed to what the latest one is.
Please show the declaration of categories and newCategory. I would even recommend showing the entire managed bean and the declaration of the datatable. I don't understand what you mean by "every previous entry gets changed to what the latest one is".
Hi, Eric
Hi !!
What I mean is that when I add History
it appears 'History'
when I add geography, it overwrites that first history and appears
'Geography',
'Geography'
If I then added music, it overwrites all 3 and appears,
'Music',
'Music',
'Music'
your list is of what type?
private List<?????> categories
14:01
Yup
private List<Category> categories;
And your object Category has a property like :
private String name;
which can take 'History','Geography',etc... ?
Yeah.
can you paste here the declaration of your datatable please?
<p:dataTable id="categoryTable" var="category" value="#{categoryController.categories}">

<f:facet name="header">
Category List
</f:facet>


<p:column headerText="Category ID">
<h:outputText value="#{category.categoryID}"/>
</p:column>

<p:column headerText="Category">
<h:outputText value="#{category.name}"/>
</p:column>

<f:facet name="footer">
<p:commandButton value="Add Category" oncomplete="newCategoryDialog.show()"
icon="ui-icon-plusthick" title="Adds new category"/>
<p:commandButton value="Delete Category" actionListener=
What database do you use?
14:07
MySQL
With Hibernate link
and if you take a look into your database directly, do you see all the rows updated with the same category name you entered?
the last category name*
No, they are correct. Each one is unique.
Which is strange because they both appear in the same place:
public void addCategory() {
    categoryRepository.insert(newCategory);
    categories.add(newCategory);
}
Can you paste the complete managed bean please, I don't see what's happening there...
package uk.ac.hw.services.collabquiz.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import uk.ac.hw.services.collabquiz.dao.ICategoryRepository;
import uk.ac.hw.services.collabquiz.entities.Category;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import java.util.ArrayList;
import java.util.List;

@Controller("categoryController")
@ManagedBean(name = "categoryController")
this should work, its strange... you can try to add rowKey="#{category.categoryID}" to the datatable but i don't think it will change anything...
or you can try making a new call to the database after the insert but this is not very good for performance...
public void addCategory() {
categoryRepository.insert(newCategory);
categories = categoryRepository.list();
}
14:26
Hm.
Why isn't it just working :S
Another thing, you're using scopes from javax.faces.bean but using spring at the same time. I do the same and found there was a strange behaviour when doing this. Since then I use spring scopes
Okay, will bare that in mind. Is there any way to view this chat after I close it, Eric?
For the moment it appears on the comments of your question. I don't know if this will stay for long after the discussion is closed
Sorry I cannot do more for you... It's a very strange behavior. I don't see why it would change the category.name of all your objects in the list when inserting a new one... It must be something very obvious that we missed ^^. Good luck for that !!
Did you see that last dude's comment? Trying it out now.
Yeah it worked.
hehe well done. Should have thought about that, I do it in all my beans ^^. Good
14:41
So there's not excess object creation?
no*
I guess Java garbage collects automatically
I don't really know the reason. I'm kind of new to jsf ^^
okay
Thanks for all the help Eric!
That hide option for submit though
is there anyway to set the textfields to empty?
14:56
Don't get what you mean !!
if you want to add another category it should be ok if you set newCategory = new Category(), it should empty all the fields !
and update the form of course ^^
yeah
All I was meaning is that the onComplete="newCategoryDialog.hide()" doesn't actually clear the data from the form
nope, it just hides the dialog. But updating the form will force the input fields to retrieve their values and since newCategory will be a brand new object... I hope ^^
May I ask where you are from?
Edinburgh
nah it's retaining values from the last submit
15:13
Sorry for the silly question but don't you have two forms (one for the datatable and one for the dialog) and are you updating the right one?
You're right, I might have been referring to the wrong one
it works now?
I haven't tried it. Next priority is to get a checkbox working so rows can be selected and then deleted :D
with a popup dialog asking to confirm of course ^^.
15:35
Yup.
After adding checkboxes to the first column nothing can be added. What the hell is up with that
how did you do it?
(added the checkboxes)
<p:column selectionMode="multiple" style="width:18px" /> just before the ID column.

last day (15 days later) »