mirror of
https://github.com/hibernate/hibernate-orm
synced 2025-02-17 16:44:57 +00:00
HHH-16193 LazyInitializationException when accessing loaded reference after session is closed using bytecode enhancement
This commit is contained in:
parent
4470e74da3
commit
46c9da1a46
@ -12,11 +12,13 @@
|
||||
import org.hibernate.PersistentObjectException;
|
||||
import org.hibernate.TypeMismatchException;
|
||||
import org.hibernate.action.internal.DelayedPostInsertIdentifier;
|
||||
import org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor;
|
||||
import org.hibernate.cache.spi.access.EntityDataAccess;
|
||||
import org.hibernate.cache.spi.access.SoftLock;
|
||||
import org.hibernate.engine.spi.EntityKey;
|
||||
import org.hibernate.engine.spi.PersistenceContext;
|
||||
import org.hibernate.engine.spi.PersistentAttributeInterceptable;
|
||||
import org.hibernate.engine.spi.PersistentAttributeInterceptor;
|
||||
import org.hibernate.engine.spi.SessionImplementor;
|
||||
import org.hibernate.event.spi.EventSource;
|
||||
import org.hibernate.event.spi.LoadEvent;
|
||||
@ -38,6 +40,9 @@
|
||||
import org.hibernate.proxy.LazyInitializer;
|
||||
import org.hibernate.stat.spi.StatisticsImplementor;
|
||||
|
||||
import static org.hibernate.engine.internal.ManagedTypeHelper.asPersistentAttributeInterceptable;
|
||||
import static org.hibernate.engine.internal.ManagedTypeHelper.isPersistentAttributeInterceptable;
|
||||
|
||||
/**
|
||||
* Defines the default load event listeners used by hibernate for loading entities
|
||||
* in response to generated load events.
|
||||
@ -530,7 +535,16 @@ private Object doLoad(LoadEvent event, EntityPersister persister, EntityKey keyT
|
||||
= CacheEntityLoaderHelper.INSTANCE.loadFromSessionCache( event, keyToLoad, options );
|
||||
final Object entity = persistenceContextEntry.getEntity();
|
||||
if ( entity != null ) {
|
||||
return persistenceContextEntry.isManaged() ? entity : null;
|
||||
if ( persistenceContextEntry.isManaged() ) {
|
||||
if ( isPersistentAttributeInterceptable( entity ) ) {
|
||||
final PersistentAttributeInterceptor interceptor = asPersistentAttributeInterceptable( entity ).$$_hibernate_getInterceptor();
|
||||
if ( interceptor instanceof EnhancementAsProxyLazinessInterceptor ) {
|
||||
( (EnhancementAsProxyLazinessInterceptor) interceptor ).forceInitialize( entity, null );
|
||||
}
|
||||
}
|
||||
return entity;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
else {
|
||||
return load( event, persister, keyToLoad );
|
||||
|
@ -70,7 +70,6 @@ public void resolveInstance(RowProcessingState rowProcessingState) {
|
||||
entityInstance = loadingEntityEntry.getEntityInstance();
|
||||
}
|
||||
else {
|
||||
if ( entityInstance == null ) {
|
||||
// Force creating a proxy
|
||||
entityInstance = session.internalLoad(
|
||||
entityKey.getEntityName(),
|
||||
@ -82,7 +81,6 @@ public void resolveInstance(RowProcessingState rowProcessingState) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEntityInitialized() {
|
||||
|
@ -10,10 +10,8 @@
|
||||
|
||||
import org.hibernate.FetchNotFoundException;
|
||||
import org.hibernate.annotations.NotFoundAction;
|
||||
import org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor;
|
||||
import org.hibernate.engine.spi.EntityKey;
|
||||
import org.hibernate.engine.spi.PersistenceContext;
|
||||
import org.hibernate.engine.spi.PersistentAttributeInterceptor;
|
||||
import org.hibernate.engine.spi.SharedSessionContractImplementor;
|
||||
import org.hibernate.internal.log.LoggingHelper;
|
||||
import org.hibernate.internal.util.StringHelper;
|
||||
@ -33,8 +31,6 @@
|
||||
import org.hibernate.sql.results.graph.entity.LoadingEntityEntry;
|
||||
import org.hibernate.sql.results.jdbc.spi.RowProcessingState;
|
||||
|
||||
import static org.hibernate.engine.internal.ManagedTypeHelper.asPersistentAttributeInterceptable;
|
||||
import static org.hibernate.engine.internal.ManagedTypeHelper.isPersistentAttributeInterceptable;
|
||||
import static org.hibernate.internal.log.LoggingHelper.toLoggableString;
|
||||
|
||||
/**
|
||||
@ -138,12 +134,6 @@ public void initializeInstance(RowProcessingState rowProcessingState) {
|
||||
final PersistenceContext persistenceContext = session.getPersistenceContextInternal();
|
||||
entityInstance = persistenceContext.getEntity( entityKey );
|
||||
if ( entityInstance != null ) {
|
||||
if ( isPersistentAttributeInterceptable( entityInstance ) ) {
|
||||
final PersistentAttributeInterceptor interceptor = asPersistentAttributeInterceptable( entityInstance ).$$_hibernate_getInterceptor();
|
||||
if ( interceptor instanceof EnhancementAsProxyLazinessInterceptor ) {
|
||||
( (EnhancementAsProxyLazinessInterceptor) interceptor ).forceInitialize( entityInstance, null );
|
||||
}
|
||||
}
|
||||
isInitialized = true;
|
||||
return;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user