HHH-16673 Fail to get access lazy fetched field ( @ManyToOne ) wich is part of a composite Id (using an @IdClass) when stored in L2 cache

This commit is contained in:
Andrea Boriero 2023-06-08 12:12:04 +02:00 committed by Andrea Boriero
parent 036631f42c
commit a64748ba84
1 changed files with 17 additions and 4 deletions

View File

@ -41,10 +41,9 @@ import org.hibernate.sql.results.graph.DomainResultCreationState;
import org.hibernate.sql.results.graph.Fetch; import org.hibernate.sql.results.graph.Fetch;
import org.hibernate.sql.results.graph.FetchParent; import org.hibernate.sql.results.graph.FetchParent;
import org.hibernate.sql.results.graph.Fetchable; import org.hibernate.sql.results.graph.Fetchable;
import org.hibernate.sql.results.graph.embeddable.internal.EmbeddableFetchImpl;
import org.hibernate.sql.results.graph.embeddable.internal.EmbeddableResultImpl;
import org.hibernate.sql.results.graph.embeddable.internal.NonAggregatedIdentifierMappingFetch; import org.hibernate.sql.results.graph.embeddable.internal.NonAggregatedIdentifierMappingFetch;
import org.hibernate.sql.results.graph.embeddable.internal.NonAggregatedIdentifierMappingResult; import org.hibernate.sql.results.graph.embeddable.internal.NonAggregatedIdentifierMappingResult;
import org.hibernate.sql.results.graph.entity.LoadingEntityEntry;
/** /**
* A "non-aggregated" composite identifier. * A "non-aggregated" composite identifier.
@ -274,8 +273,7 @@ public class NonAggregatedIdentifierMappingImpl extends AbstractCompositeIdentif
Object o = mappedIdAttributeMapping.getPropertyAccess().getGetter().get( id ); Object o = mappedIdAttributeMapping.getPropertyAccess().getGetter().get( id );
if ( attribute instanceof ToOneAttributeMapping && !( mappedIdAttributeMapping instanceof ToOneAttributeMapping ) ) { if ( attribute instanceof ToOneAttributeMapping && !( mappedIdAttributeMapping instanceof ToOneAttributeMapping ) ) {
final ToOneAttributeMapping toOneAttributeMapping = (ToOneAttributeMapping) attribute; final ToOneAttributeMapping toOneAttributeMapping = (ToOneAttributeMapping) attribute;
final EntityPersister entityPersister = toOneAttributeMapping.getEntityMappingType() final EntityPersister entityPersister = toOneAttributeMapping.getEntityMappingType().getEntityPersister();
.getEntityPersister();
final EntityKey entityKey = session.generateEntityKey( o, entityPersister ); final EntityKey entityKey = session.generateEntityKey( o, entityPersister );
final PersistenceContext persistenceContext = session.getPersistenceContext(); final PersistenceContext persistenceContext = session.getPersistenceContext();
// it is conceivable there is a proxy, so check that first // it is conceivable there is a proxy, so check that first
@ -290,6 +288,21 @@ public class NonAggregatedIdentifierMappingImpl extends AbstractCompositeIdentif
toOneAttributeMapping.getAttributeName() toOneAttributeMapping.getAttributeName()
); );
} }
if ( o == null ) {
final LoadingEntityEntry loadingEntityEntry = persistenceContext.getLoadContexts()
.findLoadingEntityEntry( entityKey );
if ( loadingEntityEntry != null ) {
o = loadingEntityEntry.getEntityInstance();
}
else {
o = session.internalLoad(
entityPersister.getEntityName(),
entityKey.getIdentifier(),
true,
true
);
}
}
} }
} }
propertyValues[i] = o; propertyValues[i] = o;