更新后JPA OneToMany问题
我有 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为正确的方法实际上是加载现有的 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.