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 c57f65fde4..0fac090a7d 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 @@ -2646,6 +2646,26 @@ public abstract class BaseSqmToSqlAstConverter extends Base if ( selectionNodeType instanceof PluralPersistentAttribute ) { sqmExpressable = ( (PluralPersistentAttribute) selectionNodeType ).getElementPathSource(); } + else if ( selectionNodeType instanceof SqmPathSource) { + final SqmPathSource pathSource = (SqmPathSource) selectionNodeType; + final CollectionPart.Nature partNature = CollectionPart.Nature.fromName( + pathSource.getPathName() + ); + if ( partNature == null ) { + sqmExpressable = selectionNodeType; + } + else { + final SqmPath sqmPath = (SqmPath) subQuerySelection.getSelectableNode(); + final NavigablePath navigablePath = sqmPath.getNavigablePath().getParent(); + if ( navigablePath.getParent() != null ) { + final TableGroup parentTableGroup = findTableGroup( navigablePath.getParent() ); + final PluralAttributeMapping pluralPart = (PluralAttributeMapping) parentTableGroup.getModelPart() + .findSubPart( navigablePath.getUnaliasedLocalName(), null ); + return pluralPart.findSubPart( pathSource.getPathName(), null ); + } + return findTableGroup( navigablePath ).getModelPart(); + } + } else { sqmExpressable = selectionNodeType; }