开发者

Entity CRUD w/ OneToMany and orphanRemoval

开发者 https://www.devze.com 2023-04-09 09:28 出处:网络
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

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,开发者_JS百科 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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号