@ManyToOne JPA association and cascade… not sure what happens if I delete an object

前端 未结 2 1962
离开以前
离开以前 2021-01-12 10:42

I\'m still not very clear about the way cascade works in deletion operations. I was wondering what happens if I have this:

class myBean{

    @ManyToOne(fetc         


        
2条回答
  •  爱一瞬间的悲伤
    2021-01-12 11:02

    Your JPA provider isn't going to manage your java collections in memory for you. If you have a relationship mapped in both sides, then one side should be defined as the owning side using the mappedBy attribute in the mapping of the non-owning side. It's up to you to decide which side is the owning side. Once you've made that decision, the JPA provider will keep things up to date in the database using whatever cascade/orphan management you've defined in the mapping but you are left to manage your collections in the non-owning side.

    One way to manage this is to have a service method that is used to make the change and that service method takes care of updating the collection or the reference in your 1-n and n-1 side of things so your java objects are correct in memory.

提交回复
热议问题