How to resolve [A collection with cascade=“all-delete-orphan” was no longer referenced by the owning entity instance]

If you have come across the above error, chances are :

  1. You have two entities, a Parent and its Child.
  2. You are deleting (Or removing some old and adding new) Child records and updating the Parent.

Going through this post will help you resolve this error and provide you insight on the details of the solution

Entity Definitions

The Parent and Child entities which are being used in your application code would look as below.

Parent.java

package com;
 
@Entity
class Parent {
 
@OneToMany(mappedBy="parent", cascade={CascadeType.PERSIST, CascadeType.MERGE, 
           CascadeType.REFRESH}, orphanRemoval=true)
private List children;
 
public List getChildren(){
    return children;
}
public void setChildren(List list){
    children = list
}

Child.java

package com;
 
@Entity
class Child {
@ManyToOne
@JoinColumn(name = "parentid")
   Parent parent;
}

The logic or the piece of code which is generating the error would be somewhat like :

//fetch parent record from database
Parent parent = findById(parentId);
// create new child
Child child = new Child();
//create new children list
List children = new ArrayList();
//add child to the list of children
children.add(child);
//clear existing children list so that they are removed from database
parent.getChildren.clear();
//set the new children list created above
parent.setChildren(children);
//update parent
update(parent);

When the above code executes, you get an error like:

org.hibernate.HibernateException: A collection with cascade=”all-delete-orphan” was no longer referenced by the owning entity instance: com.children

Solution

Slightly change the method of associating the list of children after clearing it from the parent. That is, replace the following lines of code

//clear existing children list so that they are removed from database
parent.getChildren.clear();
//set the new children list created above
parent.setChildren(children);

to

//clear existing children list so that they are removed from database
parent.getChildren.clear();
//add the new children list created above to the existing list
parent.getChildren.addAll(children);

Reason

In the first approach, you clear the existing collection of children associated with the parent entity, create a new collection(ArrayList) and assign it to the child list of the parent. The previous collection is now not associated with any entity but it remains in the session. The error A collection with cascade=“all-delete-orphan” was no longer referenced by the owning entity instance says it all.
In the second approach, you clear the existing collection of children associated with the parent entity, create a new collection(ArrayList) but instead of assigning the new collection to child list, add the new collection to this list. Now, in this case, the collection of child entities still remains associated with an entity(the parent entity).
Therefore, when you try to update, Hibernate(or JPA) finds no problem and updates both the parent and its children successfully.

Hope this post helped you to resolve the error !!! Don’t forget to click the clap below and share for others to benefit from it.

107

6
Leave a Reply

Mark Your Impression

  Subscribe  
newest oldest most voted
Notify of
Mark
Guest
Mark

Well explained…Helped me a lot !!!

Anonymous
Guest
Anonymous

goog job

SASIKUMAR CHANDRAN
Guest
SASIKUMAR CHANDRAN

Nice explanation, my issue resolved, thank you.

Close Menu

Never Miss an article !

Get the new post delivered straight into your inbox, enter your email and hit the button

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

codippa will use the information you provide on this form to be in touch with you and to provide updates and marketing.