From 663bbc81d706780a751713e50d5cdf136cadf867 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Thu, 27 Jun 2019 16:59:26 +0100 Subject: [PATCH] HHH-11147 Fix initializing an entity that does not exist --- .../entity/AbstractEntityPersister.java | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java b/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java index 2f2ccbd76d..9e715e7a42 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java @@ -75,6 +75,7 @@ import org.hibernate.engine.spi.EntityKey; import org.hibernate.engine.spi.ExecuteUpdateResultCheckStyle; import org.hibernate.engine.spi.LoadQueryInfluencers; import org.hibernate.engine.spi.Mapping; +import org.hibernate.engine.spi.PersistenceContext; import org.hibernate.engine.spi.PersistenceContext.NaturalIdHelper; import org.hibernate.engine.spi.PersistentAttributeInterceptable; import org.hibernate.engine.spi.PersistentAttributeInterceptor; @@ -4311,16 +4312,28 @@ public abstract class AbstractEntityPersister if ( currentInterceptor instanceof EnhancementAsProxyLazinessInterceptor ) { final EnhancementAsProxyLazinessInterceptor proxyInterceptor = (EnhancementAsProxyLazinessInterceptor) currentInterceptor; - readLockLoader.load( - proxyInterceptor.getEntityKey().getIdentifier(), + final EntityKey entityKey = proxyInterceptor.getEntityKey(); + final Serializable identifier = entityKey.getIdentifier(); + final Object loaded = readLockLoader.load( + identifier, entity, session, LockOptions.READ ); + if ( loaded == null ) { + final PersistenceContext persistenceContext = session.getPersistenceContext(); + persistenceContext.removeEntry( entity ); + persistenceContext.removeEntity( entityKey ); + session.getFactory().getEntityNotFoundDelegate().handleEntityNotFound( + entityKey.getEntityName(), + identifier + ); + } + final LazyAttributeLoadingInterceptor interceptor = enhancementMetadata.injectInterceptor( entity, - proxyInterceptor.getEntityKey().getIdentifier(), + identifier, session );