diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/select/SqmQueryGroup.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/select/SqmQueryGroup.java index f90c532e59..9f2fdd738d 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/select/SqmQueryGroup.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/select/SqmQueryGroup.java @@ -170,7 +170,8 @@ public class SqmQueryGroup extends SqmQueryPart implements JpaQueryGroup firstSqmSelection = typedNodes.get( j ); final JavaType firstJavaType = firstSqmSelection.getNodeJavaType(); - if ( firstJavaType != selections.get( j ).getNodeJavaType() ) { + final JavaType nodeJavaType = selections.get( j ).getNodeJavaType(); + if ( nodeJavaType != null && firstJavaType != null && firstJavaType != nodeJavaType ) { throw new SemanticException( "Select items of the same index must have the same java type across all query parts" ); diff --git a/hibernate-core/src/main/java/org/hibernate/sql/results/jdbc/internal/JdbcValuesMappingProducerProviderStandard.java b/hibernate-core/src/main/java/org/hibernate/sql/results/jdbc/internal/JdbcValuesMappingProducerProviderStandard.java index fa95470f6d..151bdb7fc1 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/results/jdbc/internal/JdbcValuesMappingProducerProviderStandard.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/results/jdbc/internal/JdbcValuesMappingProducerProviderStandard.java @@ -9,9 +9,15 @@ package org.hibernate.sql.results.jdbc.internal; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.query.results.ResultSetMapping; import org.hibernate.query.results.ResultSetMappingImpl; +import org.hibernate.sql.ast.spi.SqlSelection; +import org.hibernate.sql.ast.tree.select.QueryGroup; +import org.hibernate.sql.ast.tree.select.QueryPart; import org.hibernate.sql.ast.tree.select.SelectStatement; import org.hibernate.sql.results.jdbc.spi.JdbcValuesMappingProducer; import org.hibernate.sql.results.jdbc.spi.JdbcValuesMappingProducerProvider; +import org.hibernate.type.descriptor.jdbc.NullJdbcType; + +import java.util.List; /** * Standard JdbcValuesMappingProducerProvider implementation @@ -28,10 +34,20 @@ public class JdbcValuesMappingProducerProviderStandard implements JdbcValuesMapp public JdbcValuesMappingProducer buildMappingProducer( SelectStatement sqlAst, SessionFactoryImplementor sessionFactory) { - return new JdbcValuesMappingProducerStandard( - sqlAst.getQuerySpec().getSelectClause().getSqlSelections(), - sqlAst.getDomainResultDescriptors() - ); + return new JdbcValuesMappingProducerStandard( getSelections( sqlAst ), sqlAst.getDomainResultDescriptors() ); + } + + private static List getSelections(SelectStatement selectStatement) { + if ( selectStatement.getQueryPart() instanceof QueryGroup ) { + final QueryGroup queryGroup = (QueryGroup) selectStatement.getQueryPart(); + for ( QueryPart queryPart : queryGroup.getQueryParts() ) { + if ( !(queryPart.getFirstQuerySpec().getSelectClause().getSqlSelections() + .get( 0 ).getExpressionType().getSingleJdbcMapping().getJdbcType() instanceof NullJdbcType) ) { + return queryPart.getFirstQuerySpec().getSelectClause().getSqlSelections(); + } + } + } + return selectStatement.getQuerySpec().getSelectClause().getSqlSelections(); } @Override