diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/SqmUtil.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/SqmUtil.java index 4cae539194..cca8ee9a45 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/SqmUtil.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/SqmUtil.java @@ -246,7 +246,7 @@ public class SqmUtil { * or one that has an explicit on clause predicate. */ public static boolean isFkOptimizationAllowed(SqmPath sqmPath, EntityAssociationMapping associationMapping) { - if ( sqmPath instanceof SqmJoin sqmJoin ) { + if ( associationMapping.isFkOptimizationAllowed() && sqmPath instanceof SqmJoin sqmJoin ) { switch ( sqmJoin.getSqmJoinType() ) { case LEFT: if ( isFiltered( associationMapping ) ) { 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 131669c020..f58139ac0b 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 @@ -4140,12 +4140,11 @@ public abstract class BaseSqmToSqlAstConverter extends Base if ( inferredEntityMapping == null ) { // When the inferred mapping is null, we try to resolve to the FK by default, which is fine because // expansion to all target columns for select and group by clauses is handled in EntityValuedPathInterpretation - if ( entityValuedModelPart instanceof EntityAssociationMapping - && isFkOptimizationAllowed( path, (EntityAssociationMapping) entityValuedModelPart ) ) { + if ( entityValuedModelPart instanceof EntityAssociationMapping associationMapping + && isFkOptimizationAllowed( path, associationMapping ) ) { // If the table group uses an association mapping that is not a one-to-many, // we make use of the FK model part - unless the path is a non-optimizable join, // for which we should always use the target's identifier to preserve semantics - final EntityAssociationMapping associationMapping = (EntityAssociationMapping) entityValuedModelPart; final ModelPart targetPart = associationMapping.getForeignKeyDescriptor().getPart( associationMapping.getSideNature() );