Skip to content

Commit c1d9ac0

Browse files
committed
HHH-18565 Mark bytecode enhanced lazy entities as RESOLVED
1 parent 1a03011 commit c1d9ac0

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/internal/EntityInitializerImpl.java

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -641,9 +641,9 @@ protected void resolveInstanceSubInitializers(EntityInitializerData data) {
641641
}
642642
}
643643
final RowProcessingState rowProcessingState = data.getRowProcessingState();
644-
assert entityEntry == rowProcessingState.getSession()
645-
.getPersistenceContextInternal()
646-
.getEntry( data.entityInstanceForNotify );
644+
final PersistenceContext persistenceContext = rowProcessingState.getSession()
645+
.getPersistenceContextInternal();
646+
assert entityEntry == persistenceContext.getEntry( data.entityInstanceForNotify );
647647
final Object[] loadedState = entityEntry.getLoadedState();
648648
final Object[] state;
649649
if ( loadedState == null ) {
@@ -939,9 +939,26 @@ public void resolveInstance(Object instance, EntityInitializerData data) {
939939
&& ( (ToOneAttributeMapping) referencedModelPart ).getSideNature() == TARGET;
940940
return;
941941
}
942-
// If the entity initializer is null, we know the entity is fully initialized,
943-
// otherwise it will be initialized by some other initializer
944-
data.setState( data.entityHolder.getEntityInitializer() == null ? State.INITIALIZED : State.RESOLVED );
942+
if ( data.concreteDescriptor.getBytecodeEnhancementMetadata().isEnhancedForLazyLoading()
943+
&& isPersistentAttributeInterceptable( data.entityInstanceForNotify )
944+
&& getAttributeInterceptor( data.entityInstanceForNotify ) instanceof EnhancementAsProxyLazinessInterceptor enhancementInterceptor
945+
&& !enhancementInterceptor.isInitialized() ) {
946+
data.setState( State.RESOLVED );
947+
}
948+
else {
949+
// If the entity initializer is null, we know the entity is fully initialized,
950+
// otherwise it will be initialized by some other initializer
951+
data.setState( data.entityHolder.getEntityInitializer() == null ? State.INITIALIZED : State.RESOLVED );
952+
}
953+
954+
if ( data.getState() == State.RESOLVED ) {
955+
data.entityHolder = persistenceContext.claimEntityHolderIfPossible(
956+
data.entityKey,
957+
data.entityInstanceForNotify,
958+
rowProcessingState.getJdbcValuesSourceProcessingState(),
959+
this
960+
);
961+
}
945962
}
946963
else if ( lazyInitializer.isUninitialized() ) {
947964
data.setState( State.RESOLVED );
@@ -1751,6 +1768,10 @@ protected void forEachSubInitializer(BiConsumer<Initializer<?>, RowProcessingSta
17511768
}
17521769
}
17531770

1771+
private static PersistentAttributeInterceptor getAttributeInterceptor(Object entity) {
1772+
return asPersistentAttributeInterceptable( entity ).$$_hibernate_getInterceptor();
1773+
}
1774+
17541775
@Override
17551776
public String toString() {
17561777
return "EntityJoinedFetchInitializer(" + LoggingHelper.toLoggableString( getNavigablePath() ) + ")";

0 commit comments

Comments
 (0)