带有 OneToMany 和 orphanRemoval 的实体 CRUD
我正在为 Web 应用程序构建一些 CRUD 表单。我有一个实体,比如说 Part,它有自己的字段(sku、价格等)以及通过 @OneToMany 到其他实体的链接。我布置 CRUD 界面的方式是在一个表单上编辑零件数据点,并在一个单独的表单(在不同的选项卡上)编辑相关数据点。
当我提交零件表单时,它会发布零件 ID、价格、sku 等,但不会发布任何相关数据。当 Spring 进行数据绑定时,它将调用我的 Part 类上的无参数构造函数,然后调用 setPrice、setId、setSku 等。相关实体不会发布任何内容,因为它们位于单独的表单上。然后使用 JPA 合并 Spring 提供给我的部件对象。
问题是我在 OneToMany 关系上使用cascade = ALL, orphanRemoval = true。所有这些关系都被删除,因为它们在数据绑定器生成的零件对象中为空。可能的解决方案:
- 我可以让 Spring 以某种方式从 JPA 中提取部件,而不是调用无参数构造函数吗?
- 获取数据绑定器生成的对象并将所有部件属性复制到从数据库中提取的对象中
- 不要使用数据绑定;从 servlet 请求中提取参数
显然第一个是最优选的;有办法做到这一点吗?遇到类似情况你是如何处理的?
I am building some CRUD forms for a web application. I have one entity, lets say Part, that has its own fields (sku, price, etc) as well as a link to other entities via @OneToMany. The way I have laid out my CRUD interface is that the part data points are edited on one form, and the related data points are edited in a separate form (on a different tab).
When I submit the Part form it will POST the part ID, price, sku, etc, but none of the related data. When Spring does its data binding, it will call the no-argument constructor on my Part class, then call setPrice, setId, setSku, etc. Nothing is posted for the related entities since they are on a separate form. The part object provided to me by Spring is then merged using JPA.
The problem is that I use cascade = ALL, orphanRemoval = true on the OneToMany relationships. All these relationships are deleted because they are blank in the data-binder-generated part object. Possible solutions:
- Can I have Spring somehow pull the Part from JPA instead of calling the no-arg constructor?
- Take the data-binder-generated object and copy all of the part attributes to an object pulled from the database
- Do not use data binding; pull parameters out of the servlet request
Obviously the first one is the most preferred; is there a way to accomplish this? How have you dealed with similar situations?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论