From d99c7acf633dddffdcd9e5ef824b42fe9ba0d840 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Thu, 8 Jun 2023 12:12:04 +0200 Subject: [PATCH] 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 --- .../NonAggregatedIdentifierMappingImpl.java | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/NonAggregatedIdentifierMappingImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/NonAggregatedIdentifierMappingImpl.java index a25d4af054..3e2a618ad4 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/NonAggregatedIdentifierMappingImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/NonAggregatedIdentifierMappingImpl.java @@ -41,10 +41,9 @@ import org.hibernate.sql.results.graph.DomainResultCreationState; import org.hibernate.sql.results.graph.Fetch; import org.hibernate.sql.results.graph.FetchParent; 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.NonAggregatedIdentifierMappingResult; +import org.hibernate.sql.results.graph.entity.LoadingEntityEntry; /** * A "non-aggregated" composite identifier. @@ -274,8 +273,7 @@ public class NonAggregatedIdentifierMappingImpl extends AbstractCompositeIdentif Object o = mappedIdAttributeMapping.getPropertyAccess().getGetter().get( id ); if ( attribute instanceof ToOneAttributeMapping && !( mappedIdAttributeMapping instanceof ToOneAttributeMapping ) ) { final ToOneAttributeMapping toOneAttributeMapping = (ToOneAttributeMapping) attribute; - final EntityPersister entityPersister = toOneAttributeMapping.getEntityMappingType() - .getEntityPersister(); + final EntityPersister entityPersister = toOneAttributeMapping.getEntityMappingType().getEntityPersister(); final EntityKey entityKey = session.generateEntityKey( o, entityPersister ); final PersistenceContext persistenceContext = session.getPersistenceContext(); // it is conceivable there is a proxy, so check that first @@ -290,6 +288,21 @@ public class NonAggregatedIdentifierMappingImpl extends AbstractCompositeIdentif 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;