Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
/**
* 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
Expand Down Expand Up @@ -60,7 +60,7 @@ public ResultBuilder cacheKeyInstance() {

@Override
public Class<?> getJavaType() {
return explicitJavaType.getJavaTypeClass();
return explicitJavaType == null ? null : explicitJavaType.getJavaTypeClass();
}

@Override
Expand Down Expand Up @@ -157,23 +157,14 @@ public boolean equals(Object o) {
if ( o == null || getClass() != o.getClass() ) {
return false;
}

CompleteResultBuilderBasicValuedStandard that = (CompleteResultBuilderBasicValuedStandard) o;

if ( !Objects.equals( explicitColumnName, that.explicitColumnName ) ) {
return false;
}
if ( !Objects.equals( explicitType, that.explicitType ) ) {
return false;
}
return explicitJavaType.equals( that.explicitJavaType );
return Objects.equals( explicitColumnName, that.explicitColumnName )
&& Objects.equals( explicitType, that.explicitType )
&& Objects.equals( explicitJavaType, that.explicitJavaType );
}

@Override
public int hashCode() {
int result = explicitColumnName != null ? explicitColumnName.hashCode() : 0;
result = 31 * result + ( explicitType != null ? explicitType.hashCode() : 0 );
result = 31 * result + explicitJavaType.hashCode();
return result;
return Objects.hash( explicitColumnName, explicitType, explicitJavaType );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ else if ( resultJavaType != null && resultJavaType != Object[].class ) {
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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* 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.dialect.H2Dialect;

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.RequiresDialect;
import org.hibernate.testing.orm.junit.Setting;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
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;
import jakarta.persistence.Table;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

/**
* @author Nathan Xu
*/
@Jpa(
annotatedClasses = NamedNativeQueryWithCountColumnTest.Sample.class,
properties = @Setting(name = AvailableSettings.GLOBALLY_QUOTED_IDENTIFIERS, value = "true")
)
@TestForIssue(jiraKey = "HHH-15070")
class NamedNativeQueryWithCountColumnTest {

@BeforeEach
public void setUp(EntityManagerFactoryScope scope) {
scope.inTransaction(
entityManager -> {
for ( int i = 0; i < 3; i++ ) {
Sample sample = new Sample( i, String.valueOf( i ) );
entityManager.persist( sample );
}
}
);
}

@AfterEach
public void tearDown(EntityManagerFactoryScope scope) {
scope.inTransaction(
entityManager ->
entityManager.createQuery( "delete from Sample" ).executeUpdate()
);
}

@Test
void testNamedNativeQuery(EntityManagerFactoryScope scope) {
scope.inTransaction(
entityManager ->
entityManager.createNamedQuery( "sample.count", Long.class )
);
}

@Test
@RequiresDialect(H2Dialect.class)
void testNamedNativeQueryExecution(EntityManagerFactoryScope scope) {
scope.inTransaction(
entityManager -> {
Long count = entityManager.createNamedQuery( "sample.count", Long.class ).getSingleResult();
assertThat( count, is( 3L ) );
} );
}

@SqlResultSetMapping(
name = "mapping",
columns = @ColumnResult(name = "cnt")
)
@NamedNativeQuery(
name = "sample.count",
resultSetMapping = "mapping",
query = "SELECT count(*) AS cnt FROM SAMPLE_TABLE"
)
@Entity(name = "Sample")
@Table(name = "SAMPLE_TABLE")
static class Sample {

@Id
Integer id;

String name;

public Sample() {
}

public Sample(Integer id, String name) {
this.id = id;
this.name = name;
}
}
}