JPA / Hibernate removing “child” entities

前端 未结 1 1492
隐瞒了意图╮
隐瞒了意图╮ 2020-12-21 14:12

I have two entity classes A and B that exist in a bidirectional one-to-many relationship respectively.

A.java:

@On         


        
相关标签:
1条回答
  • 2020-12-21 14:47

    JPA doesn't manage relations for you, so while you have called remove on b, a still has a reference to it. Since the reference has a cascade persist/merge set on it, it causes b to be resurrected in the context, undoing the remove.

    You must maintain references to keep your model in sync with what you want in the database. It is good practice to null out references to objects you are removing and set back pointers unless you are sure you can deal with potential inconsistencies such as this.

    0 讨论(0)
提交回复
热议问题