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(