HHH-15343 Fix NPE in type determination for case expression with null literal

This commit is contained in:
Christian Beikov 2022-06-15 15:43:53 +02:00
parent ff2cd561f5
commit d06eee0a5d
2 changed files with 97 additions and 1 deletions

View File

@ -56,8 +56,14 @@ public class QueryHelper {
return type2;
}
if ( type1.getExpressibleJavaType() == null ) {
return type2;
}
else if ( type2.getExpressibleJavaType() == null ) {
return type1;
}
// any other precedence rules?
if ( type2.getExpressibleJavaType().isWider( type1.getExpressibleJavaType() ) ) {
else if ( type2.getExpressibleJavaType().isWider( type1.getExpressibleJavaType() ) ) {
return type2;
}

View File

@ -0,0 +1,90 @@
package org.hibernate.orm.test.query;
import java.util.List;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertNull;
@DomainModel(
annotatedClasses = { SelectCaseWhenNullLiteralTest.Person.class }
)
@SessionFactory
@TestForIssue(jiraKey = "HHH-15343")
public class SelectCaseWhenNullLiteralTest {
@BeforeEach
public void setUp(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
Person person = new Person( 1l, "Fab" );
session.persist( person );
}
);
}
@AfterEach
public void tearDown(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery( "delete from Person" ).executeUpdate();
}
);
}
@Test
public void testSelectCaseWhenNullLiteral(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
List result = session.createQuery( "select case when 1=1 then 1 else null end from Person p" ).list();
assertThat( result.size(), is( 1 ) );
assertThat( result.get( 0 ), is( 1 ) );
}
);
}
@Entity(name = "Person")
@Table(name = "PERSON_TABLE")
public static class Person {
@Id
private Long id;
private String name;
public Person() {
}
public Person(Long id, String name) {
this.id = id;
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}