From bcbae0055266490e1256bd3659b40d3aa3f3469a Mon Sep 17 00:00:00 2001 From: Marco Belladelli Date: Mon, 11 Mar 2024 11:41:31 +0100 Subject: [PATCH] HHH-17832 Register projection usage for circular bidirectional fetch --- .../metamodel/mapping/internal/ToOneAttributeMapping.java | 7 +++++++ .../hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java | 7 ++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/ToOneAttributeMapping.java b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/ToOneAttributeMapping.java index 93275d2859..b516f39d9c 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/ToOneAttributeMapping.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/mapping/internal/ToOneAttributeMapping.java @@ -60,6 +60,7 @@ import org.hibernate.metamodel.mapping.VirtualModelPart; import org.hibernate.metamodel.model.domain.NavigableRole; import org.hibernate.persister.collection.AbstractCollectionPersister; import org.hibernate.persister.entity.AbstractEntityPersister; +import org.hibernate.persister.entity.EntityNameUse; import org.hibernate.persister.entity.EntityPersister; import org.hibernate.persister.entity.JoinedSubclassEntityPersister; import org.hibernate.property.access.spi.PropertyAccess; @@ -1300,6 +1301,12 @@ public class ToOneAttributeMapping final FromClauseAccess fromClauseAccess = creationState.getSqlAstCreationState().getFromClauseAccess(); final TableGroup tableGroup = fromClauseAccess.getTableGroup( referencedNavigablePath ); fromClauseAccess.registerTableGroup( fetchablePath, tableGroup ); + // Register a PROJECTION usage as we're effectively selecting the bidirectional association + creationState.getSqlAstCreationState().registerEntityNameUsage( + tableGroup, + EntityNameUse.PROJECTION, + entityMappingType.getEntityName() + ); return buildEntityFetchJoined( fetchParent, this, diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java index 6b26f4f1d7..7688634e99 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/BaseSqmToSqlAstConverter.java @@ -3047,7 +3047,12 @@ public abstract class BaseSqmToSqlAstConverter extends Base TableGroup tableGroup, EntityNameUse entityNameUse, String hibernateEntityName) { - registerEntityNameUsage( tableGroup, entityNameUse, hibernateEntityName, false ); + registerEntityNameUsage( + tableGroup, + entityNameUse, + hibernateEntityName, + entityNameUse.getKind() == EntityNameUse.UseKind.PROJECTION + ); } private void registerEntityNameUsage(