|
When you initialize the collection of an OneToMany relation with orphanRemoval=true with null, Hibernate fails to persist changes to a (re)loaded entity with the following exception:
org.hibernate.HibernateException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: ...
at org.hibernate.engine.internal.Collections.processDereferencedCollection(Collections.java:114)
at org.hibernate.engine.internal.Collections.processUnreachableCollection(Collections.java:67)
at org.hibernate.event.internal.AbstractFlushingEventListener.flushCollections(AbstractFlushingEventListener.java:258)
at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:103)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:55)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1258)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:1335)
Things work however, if
-
the collection is initialized with an empty collection (instead of null)
-
orphanRemoval is set to false
This may be related to HHH-7726 and/or HHH-9935, but the null collection case is not covered there.
You may find a small stripped down test project on Github https://github.com/abenneke/sandbox/tree/master/hibernate-null-collection
Thank you!
|