From 83d9c3eb93d447e4aead683f651d4e6d55a09443 Mon Sep 17 00:00:00 2001 From: Gail Badner Date: Fri, 25 May 2018 01:41:55 -0700 Subject: [PATCH] HHH-12592 - Fix merging detached enhanced entities with orphan-removal collections. (cherry picked from commit 01caa5f2b4d5dfee04016d6628a529e437894dcb) --- .../internal/CollectionLoadContext.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/hibernate-core/src/main/java/org/hibernate/engine/loading/internal/CollectionLoadContext.java b/hibernate-core/src/main/java/org/hibernate/engine/loading/internal/CollectionLoadContext.java index dcf6ee605a..5d69bb5251 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/loading/internal/CollectionLoadContext.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/loading/internal/CollectionLoadContext.java @@ -18,6 +18,7 @@ import org.hibernate.CacheMode; import org.hibernate.EntityMode; import org.hibernate.HibernateException; import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy; +import org.hibernate.bytecode.spi.BytecodeEnhancementMetadata; import org.hibernate.cache.spi.entry.CollectionCacheEntry; import org.hibernate.collection.spi.PersistentCollection; import org.hibernate.engine.spi.CollectionEntry; @@ -248,6 +249,31 @@ public class CollectionLoadContext { // } } + // The collection has been completely initialized and added to the PersistenceContext. + + if ( lce.getCollection().getOwner() != null ) { + // If the owner is bytecode-enhanced and the owner's collection value is uninitialized, + // then go ahead and set it to the newly initialized collection. + final BytecodeEnhancementMetadata bytecodeEnhancementMetadata = + persister.getOwnerEntityPersister().getInstrumentationMetadata(); + if ( bytecodeEnhancementMetadata.isEnhancedForLazyLoading() ) { + // Figure out the collection property name. + // TODO: what if the collection is in an embeddable??? + final String propertyName = persister.getRole().substring( + persister.getOwnerEntityPersister().getEntityName().length() + 1 + ); + if ( !bytecodeEnhancementMetadata.isAttributeLoaded( lce.getCollection().getOwner(), propertyName ) ) { + int propertyIndex = persister.getOwnerEntityPersister().getEntityMetamodel().getPropertyIndex( + propertyName + ); + persister.getOwnerEntityPersister().setPropertyValue( + lce.getCollection().getOwner(), + propertyIndex, + lce.getCollection() + ); + } + } + } // add to cache if: boolean addToCache =