Java 在web应用程序中保存

Java 在web应用程序中保存,java,hibernate,postgresql,set,vaadin,Java,Hibernate,Postgresql,Set,Vaadin,我有一个web应用程序(使用Vaadin、Hibernate和Postgres数据库),用户可以在其中向某个表添加数据。当用户将项目添加到此表中时,系统会要求用户输入名称、日期,并从表中选择所有相关产品。当他们单击“保存”时,我可以将名称和日期保存到数据库中,但从表中抓取项目并将其放入要添加的集合时遇到问题。表的bean类如下所示: /** Created by Hibernate **/ public class BeanClass implements Serializable {

我有一个web应用程序(使用Vaadin、Hibernate和Postgres数据库),用户可以在其中向某个表添加数据。当用户将项目添加到此表中时,系统会要求用户输入名称、日期,并从表中选择所有相关产品。当他们单击“保存”时,我可以将名称和日期保存到数据库中,但从表中抓取项目并将其放入要添加的集合时遇到问题。表的bean类如下所示:

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

当用户单击save时,名称和日期将添加到表中,但relatedProducts不会添加到关系表中(在数据库中)。我已经研究了hibernate中的cascade和Reverse,我想我必须使用它们,但不太了解如何使用。

您的相关产品集是什么数据类型?如果它是另一个Bean,那么您必须定义一个OneToMany注释

在将集合写入数据库之前,是否调试了集合是否已填充

您实际上不需要遍历整个表来获得选定的值。您可以通过table.getValue()获取所选项目

getValue()

获取选定项id或在多选模式下获取一组选定id

如果表中的数据类型与Bean中的数据类型相同,那么

toAdd.setRelatedProducts(table.getValue());

应该足够了。

相关的过程应该是一个整数的元组((1,2)、(1,4)、(1,6)),因此当用户单击“保存”时,它会将3个关系添加到1。在vaadin中,表格为组件类型。