Reputation: 1335
I have a web application (using Vaadin, Hibernate, and Postgres database) in which users can add data to a certain table. When the user adds an item to this table they are asked to enter the name, date, and selected from a table all the related products. When they click save I am able to save the name and date into the database but am having trouble grabbing the items from the table and putting them into a set to add. The bean class for the table looks like this:
/** Created by Hibernate **/
public class BeanClass implements Serializable {
private String name;
private Date date;
private Set relatedProducts = new HashSet(0);
.
.
.
}
The code I am using to save the item the user wants to add is:
BeanClass toAdd = new BeanClass();
Set temp = null
/** There is a table where the user can select all the products they want to add
* When they select an item it goes to another table, so what I am doing here
* is adding looping through latter table and adding all the items they selected
* to a set (supposedly i think this should work
**/
for (Object item : table) {
temp.add(table.getContainerProperty("id", item));
}
toAdd.setName(nameField.getValue()); //I have input fields for the name and date
toAdd.setDate(dateField.getValue());
toAdd.setRelatedProducts(temp);
hbsession.save(toAdd);
When the user clicks save the name and date is added to the table, but the relatedProducts is not added to the relationship table (in the database). I have looked into cascade and inverse in hibernate and am thinking that I will have to use these but do not quite understand how.
Upvotes: 0
Views: 98
Reputation: 1365
what datatype is your relatedProducts Set? is it another Bean, then you have to define a OneToMany annotation.
did you debug if the Set is filled before writing it to the database?
You dont really need to iterate over the whole table to get selected values. You can get the items selected via table.getValue()
getValue()
Gets the selected item id or in multiselect mode a set of selected ids.
If it's the same datatype in the table as it is in your Bean, then
toAdd.setRelatedProducts(table.getValue());
should be enough.
Upvotes: 1