diff --git a/hibernate-core/src/main/java/org/hibernate/query/results/complete/CompleteResultBuilderBasicValuedStandard.java b/hibernate-core/src/main/java/org/hibernate/query/results/complete/CompleteResultBuilderBasicValuedStandard.java index 3dc5a58363..05de47888c 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/results/complete/CompleteResultBuilderBasicValuedStandard.java +++ b/hibernate-core/src/main/java/org/hibernate/query/results/complete/CompleteResultBuilderBasicValuedStandard.java @@ -27,7 +27,7 @@ import static org.hibernate.query.results.ResultsHelper.impl; /** * ResultBuilder for scalar results defined via: * * @author Steve Ebersole @@ -60,7 +60,7 @@ public class CompleteResultBuilderBasicValuedStandard implements CompleteResultB @Override public Class getJavaType() { - return explicitJavaType.getJavaTypeClass(); + return explicitJavaType == null ? null : explicitJavaType.getJavaTypeClass(); } @Override @@ -173,7 +173,7 @@ public class CompleteResultBuilderBasicValuedStandard implements CompleteResultB public int hashCode() { int result = explicitColumnName != null ? explicitColumnName.hashCode() : 0; result = 31 * result + ( explicitType != null ? explicitType.hashCode() : 0 ); - result = 31 * result + explicitJavaType.hashCode(); + result = 31 * result + ( explicitJavaType != null ? explicitJavaType.hashCode() : 0 ); return result; } } diff --git a/hibernate-core/src/main/java/org/hibernate/query/sql/internal/NativeQueryImpl.java b/hibernate-core/src/main/java/org/hibernate/query/sql/internal/NativeQueryImpl.java index 3ec4fbadc8..f296477302 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sql/internal/NativeQueryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sql/internal/NativeQueryImpl.java @@ -278,7 +278,7 @@ public class NativeQueryImpl throw new IllegalArgumentException( "Named query exists but its result type is not compatible" ); case 1: final Class actualResultJavaType = resultSetMapping.getResultBuilders().get( 0 ).getJavaType(); - if ( !resultJavaType.isAssignableFrom( actualResultJavaType ) ) { + if ( actualResultJavaType != null && !resultJavaType.isAssignableFrom( actualResultJavaType ) ) { throw buildIncompatibleException( resultJavaType, actualResultJavaType ); } break; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/query/resultmapping/NamedNativeQueryWithCountColumnTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/query/resultmapping/NamedNativeQueryWithCountColumnTest.java new file mode 100644 index 0000000000..bd3bb7006b --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/query/resultmapping/NamedNativeQueryWithCountColumnTest.java @@ -0,0 +1,54 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html + */ +package org.hibernate.orm.test.query.resultmapping; + +import org.hibernate.cfg.AvailableSettings; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; +import org.hibernate.testing.orm.junit.Jpa; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.ColumnResult; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.NamedNativeQuery; +import jakarta.persistence.SqlResultSetMapping; + +/** + * @author Nathan Xu + */ +@Jpa( + annotatedClasses = NamedNativeQueryWithCountColumnTest.Sample.class, + properties = @Setting(name = AvailableSettings.GLOBALLY_QUOTED_IDENTIFIERS, value = "true") +) +@TestForIssue(jiraKey = "HHH-15070") +class NamedNativeQueryWithCountColumnTest { + + @Test + void testNoNullPointerExceptionThrown(EntityManagerFactoryScope scope) { + scope.inTransaction( em ->em.createNamedQuery( "sample.count", Long.class ) ); + } + + @SqlResultSetMapping( + name = "mapping", + columns = @ColumnResult( name = "cnt" ) + ) + @NamedNativeQuery( + name = "sample.count", + resultSetMapping = "mapping", + query = "SELECT count(*) AS cnt FROM Sample" + ) + @Entity(name = "Sample") + static class Sample { + + @Id + Long id; + + } +}