diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/SqmSelectionQueryImpl.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/SqmSelectionQueryImpl.java index 93ca8ea054..9f207ecb06 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/SqmSelectionQueryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/SqmSelectionQueryImpl.java @@ -63,6 +63,7 @@ import org.hibernate.query.sqm.tree.select.SqmSelection; import org.hibernate.sql.results.internal.TupleMetadata; import org.hibernate.sql.results.spi.ResultsConsumer; import org.hibernate.sql.results.spi.SingleResultConsumer; +import org.hibernate.type.descriptor.java.JavaType; import static org.hibernate.jpa.HibernateHints.HINT_CACHEABLE; import static org.hibernate.jpa.HibernateHints.HINT_CACHE_MODE; @@ -276,15 +277,15 @@ public class SqmSelectionQueryImpl extends AbstractSqmSelectionQuery return Object[].class; } else { - final SqmSelection selection = selections.get(0); + final SqmSelection selection = selections.get( 0 ); if ( isSelectionAssignableToResultType( selection, expectedResultType ) ) { - return selection.getNodeJavaType().getJavaTypeClass(); - } - else { - // let's assume there's some - // way to instantiate it - return expectedResultType; + final JavaType nodeJavaType = selection.getNodeJavaType(); + if ( nodeJavaType != null ) { + return nodeJavaType.getJavaTypeClass(); + } } + // let's assume there's some way to instantiate it + return expectedResultType; } } else if ( expectedResultType != null ) { 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 cca8ee9a45..d0f1ecbd89 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 @@ -43,6 +43,7 @@ import org.hibernate.metamodel.model.domain.SimpleDomainType; import org.hibernate.metamodel.model.domain.SingularPersistentAttribute; import org.hibernate.metamodel.model.domain.internal.EntitySqmPathSource; import org.hibernate.persister.entity.EntityPersister; +import org.hibernate.query.BindableType; import org.hibernate.query.IllegalQueryOperationException; import org.hibernate.query.IllegalSelectQueryException; import org.hibernate.query.Order; @@ -827,10 +828,15 @@ public class SqmUtil { } public static boolean isSelectionAssignableToResultType(SqmSelection selection, Class expectedResultType) { - if ( expectedResultType == null - || selection != null && selection.getSelectableNode() instanceof SqmParameter ) { + if ( expectedResultType == null ) { return true; } + else if ( selection != null && selection.getSelectableNode() instanceof SqmParameter sqmParameter ) { + final Class anticipatedClass = sqmParameter.getAnticipatedType() != null ? + sqmParameter.getAnticipatedType().getBindableJavaType() : + null; + return anticipatedClass != null && expectedResultType.isAssignableFrom( anticipatedClass ); + } else if ( selection == null || !isHqlTuple( selection ) && selection.getSelectableNode().isCompoundSelection() ) { return false;