简体   繁体   中英

Hibernate : Can we delete parent object without delete the child object using Hibernate?

Hi guys Is it possible to delete the parent object without deleting child object in Hibernate ? In My Case child has multiple parent So I just want to remove one parent object from child object.

Yes, though you must remember about two things:

1) Your child entity must allow null on the foreign key to the child

2) You must remember to NOT have CascadeType.DELETE on the @OneToMany relationship in the parent.

3) Before removing the parent, clear the children collection first:

parent.setChildren(null);
session.delete(parent);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM