Fix ToOne with Ignore not found lazy loading

This commit is contained in:
Andrea Boriero 2021-10-14 15:32:30 +02:00
parent d95dad597c
commit 73452ae61f
2 changed files with 26 additions and 14 deletions

View File

@ -1533,9 +1533,15 @@ public class MappingModelCreationHelper {
final FetchTiming fetchTiming; final FetchTiming fetchTiming;
if ( fetchStyle == FetchStyle.JOIN final boolean lazy = value.isLazy();
|| !value.isLazy() if ( lazy
|| value instanceof OneToOne && value.isNullable() && value.isUnwrapProxy()
&& entityPersister.getBytecodeEnhancementMetadata().isEnhancedForLazyLoading() ) {
fetchTiming = FetchTiming.DELAYED;
}
else if ( fetchStyle == FetchStyle.JOIN
|| !lazy
|| ( value instanceof OneToOne && value.isNullable() )
|| value instanceof ManyToOne && value.isNullable() && ( (ManyToOne) value ).isIgnoreNotFound() ) { || value instanceof ManyToOne && value.isNullable() && ( (ManyToOne) value ).isIgnoreNotFound() ) {
fetchTiming = FetchTiming.IMMEDIATE; fetchTiming = FetchTiming.IMMEDIATE;
} }

View File

@ -9,6 +9,7 @@ package org.hibernate.sql.results.graph.entity.internal;
import java.util.function.Consumer; import java.util.function.Consumer;
import org.hibernate.NotYetImplementedFor6Exception; import org.hibernate.NotYetImplementedFor6Exception;
import org.hibernate.bytecode.enhance.spi.LazyPropertyInitializer;
import org.hibernate.engine.spi.EntityKey; import org.hibernate.engine.spi.EntityKey;
import org.hibernate.engine.spi.PersistenceContext; import org.hibernate.engine.spi.PersistenceContext;
import org.hibernate.internal.log.LoggingHelper; import org.hibernate.internal.log.LoggingHelper;
@ -76,22 +77,27 @@ public class EntityDelayedFetchInitializer extends AbstractFetchParentAccess imp
final EntityKey entityKey = new EntityKey( identifier, concreteDescriptor ); final EntityKey entityKey = new EntityKey( identifier, concreteDescriptor );
final PersistenceContext persistenceContext = rowProcessingState.getSession().getPersistenceContext(); final PersistenceContext persistenceContext = rowProcessingState.getSession().getPersistenceContext();
final LoadingEntityEntry loadingEntityLocally = persistenceContext.getLoadContexts().findLoadingEntityEntry( final LoadingEntityEntry loadingEntityLocally = persistenceContext.getLoadContexts()
entityKey ); .findLoadingEntityEntry( entityKey );
if ( loadingEntityLocally != null ) { if ( loadingEntityLocally != null ) {
entityInstance = loadingEntityLocally.getEntityInstance(); entityInstance = loadingEntityLocally.getEntityInstance();
} }
else { else {
entityInstance = rowProcessingState.getSession().internalLoad( if ( referencedModelPart.isInternalLoadNullable() ) {
concreteDescriptor.getEntityName(), entityInstance = LazyPropertyInitializer.UNFETCHED_PROPERTY;
identifier, }
false, else {
referencedModelPart.isInternalLoadNullable() entityInstance = rowProcessingState.getSession().internalLoad(
); concreteDescriptor.getEntityName(),
identifier,
false,
false
);
if ( entityInstance instanceof HibernateProxy ) { if ( entityInstance instanceof HibernateProxy ) {
( (HibernateProxy) entityInstance ).getHibernateLazyInitializer() ( (HibernateProxy) entityInstance ).getHibernateLazyInitializer()
.setUnwrap( referencedModelPart.isUnwrapProxy() && concreteDescriptor.isInstrumented() ); .setUnwrap( referencedModelPart.isUnwrapProxy() && concreteDescriptor.isInstrumented() );
}
} }
} }