更新后JPA OneToMany问题

发布于 2024-09-10 19:28:03 字数 424 浏览 11 评论 0原文

我有 2 个类,父类和子类,具有 @OneToMany 关系。每个父母都有 0 个或多个孩子。

@OneToMany(fetch=FetchType.LAZY, cascade={CascadeType.REMOVE})

Parent 类还有其他字段,例如名称。

我位于容器环境中,因此我的 UI 由 JSP 组成。

当我想要更改 Parent 对象时,我必须将其传递给 servlet,然后传递给客户端浏览器。在网页中,我不想放置所有孩子的信息,我只想有一个用于父母姓名的文本字段。

当控制权返回给我时,我想创建一个具有全新名称的新 Parent 对象,并与 EntityManger 合并,而无需担心孩子们。

I have 2 Classes, Parent and Child, with a @OneToMany relationship. Every parent has 0 or more children.

@OneToMany(fetch=FetchType.LAZY, cascade={CascadeType.REMOVE})

The Parent class has also other fields like the name.

I'm in a Container env, so my UI is made of JSP.

When I want to change the Parent object I have to pass it to a servlet and down to the client browser. In the web page I don't want to put all the children information, I only whant to have a text field for the parent name.

When the control return to me, I'd like to create a new Parent object with the brand new name and to merge with the EntityManger without worrying about the children.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉城メ夜风 2024-09-17 19:28:03

我认为正确的方法实际上是加载现有的 Parent 对象,将 name 属性设置为新值,然后保存。这实际上就是你正在做的事情。

因此,您的表单需要一个用于用户可以输入的名称的字段,以及一个包含父级 ID 的隐藏字段。发布表单时,您可以通过 id 加载 Parent 对象,将名称设置为输入的值,然后保存。

I think the correct way is actually to load the existing Parent object, set the name property to the new value, and save it. That is actually what you are doing.

So your form needs one field for the name that the user can enter, and one hidden field containing the id of the parent. When the form is posted, you load the Parent object by the id, set the name to the value entered, and save.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文