Skip to content

Commit 34b479a

Browse files
committed
HHH-18631 AssertionError when loading an entity after removing another, associated entity
1 parent dd9f790 commit 34b479a

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
@@ -85,7 +85,6 @@
8585
import static org.hibernate.engine.internal.ManagedTypeHelper.asPersistentAttributeInterceptable;
8686
import static org.hibernate.engine.internal.ManagedTypeHelper.isPersistentAttributeInterceptable;
8787
import static org.hibernate.internal.util.NullnessUtil.castNonNull;
88-
import static org.hibernate.metamodel.mapping.ForeignKeyDescriptor.Nature.TARGET;
8988
import static org.hibernate.proxy.HibernateProxy.extractLazyInitializer;
9089

9190
/**
@@ -931,12 +930,15 @@ public void resolveInstance(Object instance, EntityInitializerData data) {
931930
);
932931
data.entityHolder = persistenceContext.getEntityHolder( data.entityKey );
933932
if ( data.entityHolder == null ) {
934-
// Entity was most probably removed in the same session without setting the reference to null
935-
resolveKey( data );
936-
assert data.getState() == State.MISSING;
937-
assert referencedModelPart instanceof ToOneAttributeMapping
938-
&& ( (ToOneAttributeMapping) referencedModelPart ).getSideNature() == TARGET;
939-
return;
933+
// Entity was most probably removed in the same session without setting this association to null.
934+
// Since this load request can happen through `find()` which doesn't auto-flush on association joins,
935+
// the entity must be fully initialized, even if it is removed already
936+
data.entityHolder = persistenceContext.claimEntityHolderIfPossible(
937+
data.entityKey,
938+
data.entityInstanceForNotify,
939+
rowProcessingState.getJdbcValuesSourceProcessingState(),
940+
this
941+
);
940942
}
941943
if ( data.concreteDescriptor.getBytecodeEnhancementMetadata().isEnhancedForLazyLoading()
942944
&& isPersistentAttributeInterceptable( data.entityInstanceForNotify )

0 commit comments

Comments
 (0)