2013-07-31 73 views
0

我有一个Web应用程序(使用Vaadin,Hibernate和Postgres数据库),用户可以在其中将数据添加到某个表。当用户将一个项目添加到这个表格中时,他们被要求输入名称,日期以及从表格中选择的所有相关产品。当他们点击保存时,我可以将名称和日期保存到数据库中,但无法从表格中抓取项目并将其放入要添加的集合中。 bean类的表看起来像这样:保存在Web应用程序中

/** Created by Hibernate **/ 
public class BeanClass implements Serializable { 
    private String name; 
    private Date date; 
    private Set relatedProducts = new HashSet(0); 
    . 
    . 
    . 
    } 

我使用,以节省用户想要添加项目的代码:

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); 

当用户点击保存名称和日期添加到表中,但相关产品不会添加到关系表(在数据库中)。我在冬眠期间考虑了级联和反转,并且认为我将不得不使用这些,但不太明白如何。

回答

1

什么数据类型是您relatedProducts设置?它是另一个Bean,那么你必须定义一个OneToMany注解。

如果在写入数据库之前填充了Set,那么您是否进行了调试?

你真的不需要迭代整个表来获得选定的值。你可以得到()()通过table.getValue选择的项目

的getValue

获取所选项目的ID或者多选模式一组选定的ID。

如果是在表中相同的数据类型,因为它是在Bean中,然后

toAdd.setRelatedProducts(table.getValue()); 

应该够了。

+0

relatedProucts应该是一个整数((1,2),(1,4),(1,6))的元组,因此当用户单击保存时,它将添加3与1的关系。在vaadin表中,类型组件。 – abden003