java - Hibernate - A collection with cascade=”all-delete-orphan” was no longer referenced by the owning entity instance

ID : 10454

viewed : 63

Tags : javahibernatehibernate-mappingjava

Top 5 Answer for java - Hibernate - A collection with cascade=”all-delete-orphan” was no longer referenced by the owning entity instance

vote vote

92

Check all of the places where you are assigning something to sonEntities. The link you referenced distinctly points out creating a new HashSet but you can have this error anytime you reassign the set. For example:

public void setChildren(Set<SonEntity> aSet) {     this.sonEntities = aSet; //This will override the set that Hibernate is tracking. } 

Usually you want to only "new" the set once in a constructor. Any time you want to add or delete something to the list you have to modify the contents of the list instead of assigning a new list.

To add children:

public void addChild(SonEntity aSon) {     this.sonEntities.add(aSon); } 

To remove children:

public void removeChild(SonEntity aSon) {     this.sonEntities.remove(aSon); } 
vote vote

88

The method:

public void setChildren(Set<SonEntity> aSet) {     this.sonEntities = aSet; } 

works if the parentEntity is detached and again if we update it.
But if the entity is not detached from per context, (i.e. find and update operations are in the same transaction) the below method works.

public void setChildren(Set<SonEntity> aSet) {     //this.sonEntities = aSet; //This will override the set that Hibernate is tracking.     this.sonEntities.clear();     if (aSet != null) {         this.sonEntities.addAll(aSet);     } } 
vote vote

80

When I read in various places that hibernate didn't like you to assign to a collection, I assumed that the safest thing to do would obviously be to make it final like this:

class User {   private final Set<Role> roles = new HashSet<>();  public void setRoles(Set<Role> roles) {   this.roles.retainAll(roles);   this.roles.addAll(roles); } } 

However, this doesn't work, and you get the dreaded "no longer referenced" error, which is actually quite misleading in this case.

It turns out that hibernate calls your setRoles method AND it wants its special collection class installed here, and won't accept your collection class. This had me stumped for a LONG time, despite reading all the warnings about not assigning to your collection in your set method.

So I changed to this:

public class User {   private Set<Role> roles = null;    public void setRoles(Set<Role> roles) {   if (this.roles == null) {     this.roles = roles;   } else {     this.roles.retainAll(roles);    this.roles.addAll(roles);   } } } 

So that on the first call, hibernate installs its special class, and on subsequent calls you can use the method yourself without wrecking everything. If you want to use your class as a bean, you probably need a working setter, and this at least seems to work.

vote vote

65

Actually, my problem was about equals and hashcode of my entities. A legacy code can bring a lot of problems, never forget to check it out. All I've done was just keep delete-orphan strategy and correct equals and hashcode.

vote vote

50

I had the same error. The problem for me was, that after saving the entity the mapped collection was still null and when trying to update the entity the exception was thrown. What helped for me: Saving the entity, then make a refresh (collection is no longer null) and then perform the update. Maybe initializing the collection with new ArrayList() or something might help as well.

Top 3 video Explaining java - Hibernate - A collection with cascade=”all-delete-orphan” was no longer referenced by the owning entity instance

Related QUESTION?