'A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance' exception in Hibernate

Solution Verified - Updated -

Issue

  • A OneToMany collection (e.g. Employee.tasks) marked with orphanRemoval = true is set to null and the exception below is raised when the owning entity (e.g. Employee) is flushed

    ... org.hibernate.HibernateException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: support.hibernate.entity.Employee.tasks
            at org.hibernate.engine.internal.Collections.processDereferencedCollection(Collections.java:99)
            at org.hibernate.engine.internal.Collections.processUnreachableCollection(Collections.java:50)
            at org.hibernate.event.internal.AbstractFlushingEventListener.flushCollections(AbstractFlushingEventListener.java:243)
            at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:86)
            at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:38)
            at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1295)
            at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:468)
            at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3159)
            ...
    

Environment

  • Red Hat JBoss Enterprise Application Platform (EAP) 7
  • Hibernate 5

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase, tools, and much more.

Current Customers and Partners

Log in for full access

Log In

New to Red Hat?

Learn more about Red Hat subscriptions

Using a Red Hat product through a public cloud?

How to access this content