diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/internal/EntityInitializerImpl.java b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/internal/EntityInitializerImpl.java index bef21be1bf..f19dbb01ca 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/internal/EntityInitializerImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/internal/EntityInitializerImpl.java @@ -81,6 +81,7 @@ import static org.hibernate.bytecode.enhance.spi.LazyPropertyInitializer.UNFETCH import static org.hibernate.engine.internal.ManagedTypeHelper.asPersistentAttributeInterceptable; import static org.hibernate.engine.internal.ManagedTypeHelper.isPersistentAttributeInterceptable; import static org.hibernate.internal.log.LoggingHelper.toLoggableString; +import static org.hibernate.metamodel.mapping.ForeignKeyDescriptor.Nature.TARGET; import static org.hibernate.proxy.HibernateProxy.extractLazyInitializer; /** @@ -619,6 +620,14 @@ public class EntityInitializerImpl extends AbstractInitializer implements Entity concreteDescriptor.getIdentifier( entityInstance, session ) ); entityHolder = session.getPersistenceContextInternal().getEntityHolder( entityKey ); + if ( entityHolder == null ) { + // Entity was most probably removed in the same session + resolveKey(); + assert state == State.MISSING; + assert referencedModelPart instanceof ToOneAttributeMapping + && ( (ToOneAttributeMapping) referencedModelPart ).getSideNature() == TARGET; + return; + } // If the entity initializer is null, we know the entity is fully initialized, // otherwise it will be initialized by some other initializer state = entityHolder.getEntityInitializer() == null ? State.INITIALIZED : State.RESOLVED;