HibernateException – A collection with cascade=”all-delete-orphan” was no longer referenced by the owning entity instance
The logged exception
org.hibernate.HibernateException – “A collection with cascade=”all-delete-orphan” was no longer referenced by the owning entity instance: com.onkarjoshi.hibernate.test.Person.children”
Your likely problem
Looks like you have a Parent entity and a Child entity. The Parent has a collection of Child entities with cascade=”all-delete-orphan”.
You are setting a new collection via the setter thus leaving the original collection unreferenced by the Parent entity. This doesn’t fly well with Hibernate and leaves it confused about what to do.
parent.setChilden(new HashSet<Child>()); // This won’t work. Could be an ArrayList too.
parent.getChildren().clear(); // There, fixed that!
So generally speaking, just clear out the old collection, rather than dereferencing it and creating a new one.
Tags: Hibernate, Java, JPA, ORM

Thanks a ton! This should be higher up on the google ranks.
@Matt
I hope Google has the same realization soon.
Great. thanks for the solution
Hi,
Isn’t it necessary to set the new referenced values? What If I’m updating a register for example and setting new values to the child?
Thanks a lot! This saved me!
this saved me too!
@Gustavo
Sure. You can clear it and then add new values.
Hi,
Great Solution.. Thank you so much
Great Post !! Thanx alot!
I came across same error and for me it was kind of different issue and I solved it too..just to share it here in case if u come acros my scenario.
For me this error happened, when i was tring to update the object using saveOrUpdate(), one of the sub-class key value was null and since it couldn’t find, it throwed error. once i assigned key value properly, error went away.
sreedhar, dude ! Thank you soo much. Your comment made me think outside of my self-made box. For me the entity class that created the problem wasn’t directly related to the entity class of the field that was reported in the hibernate exception. So I first spend 6 hours assuming something was somehow touching the reference that the exception reports…had nothing to do with that reference but another reference in the same class.
You, sir, win the internets for this. Thanks!
Nice. Happend to me with the JPA annotation orphanRemoval=true over a Hibernate db. Made me question the need for the orphan removal at all – which I don’t think I need after all. Thanks
Very useful- many thanks! I have been having lots of issues attempting to update Collections managed by Hibernate, and blogs like this are helping me to gradually piece together the rules I should follow!
Be careful, if the child collection is mapped as LAZY, clear() it will not work as expected.
Hi Neuquino,
I’m facing same problem and in my casen child collection is mapped as LAZY=true..
early suggestions would be appreciated.
Thanks a TON. For once, the solution I was looking for on Google was the first result.
Great Solution. Thank you so much
My child mapping is LAZY and I got around it by lazily initializing the setter.
void setSet(Set set)
{
if (this.set == null)
this.set = set;
else
{
this.set.retainAll(set);
this.set.addAll(set);
}
}
Thanks It really works..
i have little different problem but it throws the same exception. i have a collection which i have to clear and repopulate everytime i save the parent object. and the primary key in collection is a composite key (made out of three columns).
Thanks. ^^
I solve my problem.