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: , , ,
This entry was posted on Monday, September 20th, 2010 at 8:00 am and is filed under Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

34 Responses to “HibernateException – A collection with cascade=”all-delete-orphan” was no longer referenced by the owning entity instance”

  1. matt broekhuis

    Thanks a ton! This should be higher up on the google ranks.

  2. Onkar Joshi

    @Matt
    I hope Google has the same realization soon. :)

  3. Alan

    Great. thanks for the solution

  4. Gustavo Freitas

    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?

  5. cbulga

    Thanks a lot! This saved me!

  6. kab

    this saved me too!

  7. Onkar Joshi

    @Gustavo
    Sure. You can clear it and then add new values.

  8. Ravi

    Hi,

    Great Solution.. Thank you so much

  9. Aslam

    Great Post !! Thanx alot!

  10. sreedhar

    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.

  11. Jasper

    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.

  12. Nicholas Piasecki

    You, sir, win the internets for this. Thanks!

  13. Ted

    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

  14. Daniel Bryant

    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!

  15. Neuquino

    Be careful, if the child collection is mapped as LAZY, clear() it will not work as expected.

  16. Kumar

    Hi Neuquino,
    I’m facing same problem and in my casen child collection is mapped as LAZY=true..
    early suggestions would be appreciated.

  17. Nick

    Thanks a TON. For once, the solution I was looking for on Google was the first result.

  18. Ali

    Great Solution. Thank you so much

  19. Jonny

    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);
    }
    }

  20. ravi

    Thanks It really works..

  21. Tex

    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).

  22. honeymon

    Thanks. ^^
    I solve my problem.

  23. Harry

    this really works it was nice lesson for the beginner

  24. Harry

    Thank You

  25. Hibernate – A collection with cascade=”all-delete-orphan” was no longer referenced by the owning entity instance | Ask Programming & Technology

    [...] there any working solution? I’ve tried to clean the Set<..> only, according to this: How to “possible” solve the problem but it didn’t [...]

  26. Chaitra Ghadge

    Thnks for precise solution..!

  27. RouHim

    Thanks, you saved my life :)

  28. rocks

    hi,

    i have one question.. i have one entity which is self reference based child and parent entity.. i mean A has childrens of type A.

    Now If I query by the user name who has created type A as parent with child type A, then Hibernate returns 2 rows.
    1 with A(parent)- > child(A)
    2 A(child) – > parent(A)

    is there a way where hibernate can just display 1 row as A(parent)-> A(child)..

    please suggest,

  29. John

    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 roles = new HashSet();

    public void setRoles(Set 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 roles = null;

    public void setRoles(Set 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.

  30. Genetic Denim

    Howdy! I just want to offer you a huge thumbs up for your great information you have here on this post.
    I am returning to your site for more soon.

  31. dedicated hosting

    So today, I want to look at five excellent
    needs to buy web hosting instead of going the 100 % totally free path.
    You can likewise use another discount coupon to extend your affordable
    duration if you have more in your hand. Likewise, totally free hosts
    may restrict the content you place on your website.

  32. bio oil online

    I liike the valuable info you provide in your
    articles. I’ll bookmark your blog aand check again here
    regularly. I’m quite certain I’ll learn any neew stuff right here!

    Good luck foor the next!

  33. online bio oil

    Thanks a bunch for sharing this with all of us you really know what you’re speaking about!

    Bookmarked. Kindly additionally talk over with myy website =).
    We could have a hyperlink exchange arrangement among us

  34. Jagadish Cheluri

    I’m still getting “Caused by: org.hibernate.HibernateException: A collection with cascade=”all-delete-orphan” was no longer referenced by the owning entity instance exception” even after trying your solution. Can you Please help me with it.

    Here are is my code:

    Parent:
    public class InstitApplication implements java.io.Serializable {
    private Set institProjectses = new HashSet();
    @OneToMany(fetch = FetchType.LAZY, mappedBy = “institApplication”, cascade=CascadeType.ALL, orphanRemoval = true)
    public Set getInstitProjectses() {
    return this.institProjectses;
    }
    }

    Child:

    public class InstitProjects implements java.io.Serializable {
    private InstitApplication institApplication;
    @ManyToOne()
    @JoinColumns({
    @JoinColumn(name = “SEGM_ID”, referencedColumnName = “SEGM_ID”, nullable = false, insertable = false, updatable = false),
    @JoinColumn(name = “INSTITUTION_ID”, referencedColumnName = “INSTITUTION_ID”, nullable = false, insertable = false, updatable = false) })
    public InstitApplication getInstitApplication() {
    return this.institApplication;
    }
    }
    Action Class:

    institApplication.getInstitProjectses().clear();
    for(InstitProjects institProject: institProjects){
    institApplication.getInstitProjectses().add(institProject);
    }
    service.update(institApplication);

    Thanks in advance!!!

Leave a Reply

Your comment