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

This commit is contained in:
Andrea Boriero 2022-05-19 12:04:01 +02:00 committed by Andrea Boriero
parent 583a36902c
commit 5ba1193890
2 changed files with 63 additions and 21 deletions

View File

@ -157,23 +157,14 @@ public class CompleteResultBuilderBasicValuedStandard implements CompleteResultB
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 != null ? explicitJavaType.hashCode() : 0 );
return result;
return Objects.hash( explicitColumnName, explicitType, explicitJavaType );
}
}

View File

@ -7,11 +7,15 @@
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;
@ -19,6 +23,10 @@ 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
@ -30,25 +38,68 @@ import jakarta.persistence.SqlResultSetMapping;
@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 testNoNullPointerExceptionThrown(EntityManagerFactoryScope scope) {
scope.inTransaction( em ->em.createNamedQuery( "sample.count", Long.class ) );
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" )
columns = @ColumnResult(name = "cnt")
)
@NamedNativeQuery(
name = "sample.count",
resultSetMapping = "mapping",
query = "SELECT count(*) AS cnt FROM Sample"
query = "SELECT count(*) AS cnt FROM SAMPLE_TABLE"
)
@Entity(name = "Sample")
@Table(name = "SAMPLE_TABLE")
static class Sample {
@Id
Long id;
Integer id;
String name;
public Sample() {
}
public Sample(Integer id, String name) {
this.id = id;
this.name = name;
}
}
}