Bytecode enhamcement fix unnecessary query for an optional OneToOne

This commit is contained in:
Andrea Boriero 2021-10-20 09:21:29 +02:00 committed by Steve Ebersole
parent 47605bf93b
commit 9d8fbfb727
3 changed files with 12 additions and 5 deletions

View File

@ -1608,10 +1608,16 @@ public class MappingModelCreationHelper {
final FetchTiming fetchTiming;
final boolean lazy = value.isLazy();
if ( lazy
&& value.isUnwrapProxy()
&& entityPersister.getBytecodeEnhancementMetadata().isEnhancedForLazyLoading() ) {
fetchTiming = FetchTiming.DELAYED;
if ( lazy && entityPersister.getBytecodeEnhancementMetadata().isEnhancedForLazyLoading() ) {
if ( value.isUnwrapProxy() ) {
fetchTiming = FetchOptionsHelper.determineFetchTiming( fetchStyle, type, sessionFactory );
}
else if ( value instanceof ManyToOne && value.isNullable() && ( (ManyToOne) value ).isIgnoreNotFound() ) {
fetchTiming = FetchTiming.IMMEDIATE;
}
else {
fetchTiming = FetchOptionsHelper.determineFetchTiming( fetchStyle, type, sessionFactory );
}
}
else if ( fetchStyle == FetchStyle.JOIN
|| !lazy

View File

@ -53,7 +53,7 @@ public class EntityDelayedFetchImpl extends AbstractNonJoinedEntityFetch {
getEntityValuedModelPart(),
() -> new EntityDelayedFetchInitializer(
navigablePath,
(ToOneAttributeMapping) getEntityValuedModelPart(),
getEntityValuedModelPart(),
keyResult.createResultAssembler( creationState )
)
);

View File

@ -83,6 +83,7 @@ public class FetchGraphTest extends BaseNonConfigCoreFunctionalTestCase {
assert !Hibernate.isPropertyInitialized( entityD, "e" );
entityD.getE();
assertThat( stats.getPrepareStatementCount(), is( 2L ) );
assert Hibernate.isPropertyInitialized( entityD, "a" );
assert !Hibernate.isInitialized( entityD.getA() );