gpt4 book ai didi

java - 保存在 Web 应用程序中

转载 作者:行者123 更新时间:2023-12-01 14:17:37 25 4
gpt4 key购买 nike

我有一个 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);

当用户单击“保存”时,名称和日期将添加到表中,但相关产品不会添加到关系表(在数据库中)。我研究了 hibernate 中的级联和逆,我想我必须使用它们,但不太明白如何使用。

最佳答案

您的相关产品集是什么数据类型?是另一个Bean吗,那就得定义一个OneToMany注解。

在将 Set 写入数据库之前,您是否调试过 Set 是否已满?

您实际上并不需要迭代整个表来获取选定的值。您可以通过table.getValue()获取选择的项目

getValue()

Gets the selected item id or in multiselect mode a set of selected ids.

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

toAdd.setRelatedProducts(table.getValue());

应该足够了。

关于java - 保存在 Web 应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17978293/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com