2

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 1

4

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);
Sign up to request clarification or add additional context in comments.

2 Comments

any luck with the tests?
This will not work. You have to fo through all the children and set their parent null and not the other way around.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.