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.

29 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. Gianluca

    great, it works also for me!!!

    thank you so much

Leave a Reply

Your comment