HHH-15607 @NamedEntityGraph is not working when try to load Basic attribute with OneToMany relation

This commit is contained in:
Andrea Boriero 2022-10-24 17:45:19 +02:00 committed by Andrea Boriero
parent a7b87fe4eb
commit 42890e3a8d
3 changed files with 29 additions and 11 deletions

View File

@ -10,6 +10,7 @@ import org.hibernate.HibernateException;
import org.hibernate.Incubating; import org.hibernate.Incubating;
import org.hibernate.LockMode; import org.hibernate.LockMode;
import org.hibernate.LockOptions; import org.hibernate.LockOptions;
import org.hibernate.bytecode.enhance.spi.interceptor.EnhancementAsProxyLazinessInterceptor;
import org.hibernate.cache.spi.access.EntityDataAccess; import org.hibernate.cache.spi.access.EntityDataAccess;
import org.hibernate.cache.spi.entry.CacheEntry; import org.hibernate.cache.spi.entry.CacheEntry;
import org.hibernate.cache.spi.entry.ReferenceCacheEntryImpl; import org.hibernate.cache.spi.entry.ReferenceCacheEntryImpl;
@ -22,6 +23,8 @@ import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.engine.spi.EntityKey; import org.hibernate.engine.spi.EntityKey;
import org.hibernate.engine.spi.ManagedEntity; import org.hibernate.engine.spi.ManagedEntity;
import org.hibernate.engine.spi.PersistenceContext; import org.hibernate.engine.spi.PersistenceContext;
import org.hibernate.engine.spi.PersistentAttributeInterceptable;
import org.hibernate.engine.spi.PersistentAttributeInterceptor;
import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.engine.spi.SharedSessionContractImplementor;
@ -375,6 +378,18 @@ public class CacheEntityLoaderHelper {
? source.instantiate( subclassPersister, entityId ) ? source.instantiate( subclassPersister, entityId )
: instanceToLoad; : instanceToLoad;
if ( entity instanceof PersistentAttributeInterceptable ) {
PersistentAttributeInterceptor persistentAttributeInterceptor = ( (PersistentAttributeInterceptable) entity ).$$_hibernate_getInterceptor();
// if we do this after the entity has been initialized the BytecodeLazyAttributeInterceptor#isAttributeLoaded(String fieldName) would return false;
if ( persistentAttributeInterceptor == null || persistentAttributeInterceptor instanceof EnhancementAsProxyLazinessInterceptor ) {
persister.getBytecodeEnhancementMetadata().injectInterceptor(
entity,
entityId,
source
);
}
}
// make it circular-reference safe // make it circular-reference safe
TwoPhaseLoad.addUninitializedCachedEntity( TwoPhaseLoad.addUninitializedCachedEntity(
entityKey, entityKey,

View File

@ -4996,17 +4996,9 @@ public abstract class AbstractEntityPersister
if ( entity instanceof PersistentAttributeInterceptable && getRepresentationStrategy().getMode() == RepresentationMode.POJO ) { if ( entity instanceof PersistentAttributeInterceptable && getRepresentationStrategy().getMode() == RepresentationMode.POJO ) {
final BytecodeLazyAttributeInterceptor interceptor = getEntityMetamodel().getBytecodeEnhancementMetadata() final BytecodeLazyAttributeInterceptor interceptor = getEntityMetamodel().getBytecodeEnhancementMetadata()
.extractLazyInterceptor( entity ); .extractLazyInterceptor( entity );
if ( interceptor == null || interceptor instanceof EnhancementAsProxyLazinessInterceptor ) { assert interceptor != null;
getEntityMetamodel().getBytecodeEnhancementMetadata().injectInterceptor( if ( interceptor.getLinkedSession() == null ) {
entity, interceptor.setSession( session );
getIdentifier( entity, session ),
session
);
}
else {
if ( interceptor.getLinkedSession() == null ) {
interceptor.setSession( session );
}
} }
} }

View File

@ -706,6 +706,17 @@ public abstract class AbstractEntityInitializer extends AbstractFetchParentAcces
rowProcessingState rowProcessingState
); );
if ( toInitialize instanceof PersistentAttributeInterceptable ) {
PersistentAttributeInterceptor persistentAttributeInterceptor = ( (PersistentAttributeInterceptable) toInitialize ).$$_hibernate_getInterceptor();
if ( persistentAttributeInterceptor == null || persistentAttributeInterceptor instanceof EnhancementAsProxyLazinessInterceptor ) {
// if we do this after the entity has been initialized the BytecodeLazyAttributeInterceptor#isAttributeLoaded(String fieldName) would return false;
concreteDescriptor.getBytecodeEnhancementMetadata().injectInterceptor(
toInitialize,
entityIdentifier,
session
);
}
}
concreteDescriptor.setPropertyValues( toInitialize, resolvedEntityState ); concreteDescriptor.setPropertyValues( toInitialize, resolvedEntityState );
persistenceContext.addEntity( entityKey, toInitialize ); persistenceContext.addEntity( entityKey, toInitialize );