Delete child from parent and parent from child automatically with JPA annotations

后端 未结 3 576
栀梦
栀梦 2020-12-13 05:14

Suppose that we have 3 Entities object class:

class Parent {
    String name;
    List children;
}

class Child {
    String name;
    Parent pa         


        
3条回答
  •  爱一瞬间的悲伤
    2020-12-13 05:35

    orphanRemoval is delete all orphan entity example: store (s) has books(b1,b2,b3) and b1 has title(t) in this case if deleted store(s) some books(b2,b3) will be deleted. B2 and t still exist. if you use "cascade= CascadeType.Remove" just store(s) and all books will be deleted (only "t" exist).

    s->b1,b2,b3 b2->t ------after(orphanRemoval = true)--------- b2->t
    
    s->b1,b2,b3 b2->t ------ after(cascade=CascadeType.REMOVE)--------- t
    

    If orphanRemoval=true is specified the disconnected entity instance is automatically removed. This is useful for cleaning up dependent objects that should not exist without a reference from an owner object.

    If only cascade=CascadeType.REMOVE is specified no automatic action is taken since disconnecting a relationship is not a remove operation.

提交回复
热议问题