How can I delete the parent object without deleting child object in Hibernate? In my case a child has multiple parents. I just want to remove one parent object from child object.
1 Answer
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);
2 Comments
Maciej Kowalski
any luck with the tests?
Renis1235
This will not work. You have to fo through all the children and set their parent null and not the other way around.