A collection with cascade=”all-delete-orphan” was no longer referenced by the owning entity instance
April 28, 2011
Posted by on
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.