Skip to content

Commit fc1d380

Browse files
committed
HHH-18631 AssertionError when loading an entity after removing another, associated entity
1 parent c301ac8 commit fc1d380

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@
8787
import static org.hibernate.engine.internal.ManagedTypeHelper.asPersistentAttributeInterceptable;
8888
import static org.hibernate.engine.internal.ManagedTypeHelper.isPersistentAttributeInterceptable;
8989
import static org.hibernate.internal.util.NullnessUtil.castNonNull;
90-
import static org.hibernate.metamodel.mapping.ForeignKeyDescriptor.Nature.TARGET;
9190
import static org.hibernate.proxy.HibernateProxy.extractLazyInitializer;
9291

9392
/**
@@ -914,12 +913,15 @@ public void resolveInstance(Object instance, EntityInitializerData data) {
914913
);
915914
data.entityHolder = persistenceContext.getEntityHolder( data.entityKey );
916915
if ( data.entityHolder == null ) {
917-
// Entity was most probably removed in the same session without setting the reference to null
918-
resolveKey( data );
919-
assert data.getState() == State.MISSING;
920-
assert referencedModelPart instanceof ToOneAttributeMapping
921-
&& ( (ToOneAttributeMapping) referencedModelPart ).getSideNature() == TARGET;
922-
return;
916+
// Entity was most probably removed in the same session without setting this association to null.
917+
// Since this load request can happen through `find()` which doesn't auto-flush on association joins,
918+
// the entity must be fully initialized, even if it is removed already
919+
data.entityHolder = persistenceContext.claimEntityHolderIfPossible(
920+
data.entityKey,
921+
data.entityInstanceForNotify,
922+
rowProcessingState.getJdbcValuesSourceProcessingState(),
923+
this
924+
);
923925
}
924926
if ( data.concreteDescriptor.getBytecodeEnhancementMetadata().isEnhancedForLazyLoading()
925927
&& isPersistentAttributeInterceptable( data.entityInstanceForNotify )

0 commit comments

Comments
 (0)