HHH-15283 - fix NPE for NamedNativeQuery + SqlResultSetMapping (columns)

This commit is contained in:
Nathan Xu 2022-05-10 22:57:51 -04:00 committed by Andrea Boriero
parent e0f1a2ee13
commit 583a36902c
3 changed files with 58 additions and 4 deletions

View File

@ -27,7 +27,7 @@ import static org.hibernate.query.results.ResultsHelper.impl;
/**
* ResultBuilder for scalar results defined via:<ul>
* <li>JPA {@link jakarta.persistence.ColumnResult}</li>
* <li>`<return-scalar/>` as part of a `<resultset/>` stanza in `hbm.xml`</li>
* <li>`&lt;return-scalar/&gt;` as part of a `&lt;resultset/&gt;` stanza in `hbm.xml`</li>
* </ul>
*
* @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;
}
}

View File

@ -278,7 +278,7 @@ public class NativeQueryImpl<R>
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;

View File

@ -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;
}
}