HHH-13167 - When omitting the OTHERWISE clause in a CASE expression built with Criteria API, Hibernate throws a NullPointerException
This commit is contained in:
parent
fa5b632f4e
commit
33efabd597
|
@ -114,9 +114,14 @@ public class SearchedCaseExpression<R>
|
|||
.append( ( (Renderable) whenClause.getResult() ).render( renderingContext ) );
|
||||
}
|
||||
|
||||
caseStatement.append( " else " )
|
||||
.append( ( (Renderable) getOtherwiseResult() ).render( renderingContext ) )
|
||||
.append( " end" );
|
||||
Expression<?> otherwiseResult = getOtherwiseResult();
|
||||
|
||||
if ( otherwiseResult != null ) {
|
||||
caseStatement.append( " else " )
|
||||
.append( ( (Renderable) otherwiseResult ).render( renderingContext ) );
|
||||
}
|
||||
|
||||
caseStatement.append( " end" );
|
||||
|
||||
return caseStatement.toString();
|
||||
}
|
||||
|
|
|
@ -24,11 +24,13 @@ import org.hibernate.dialect.PostgreSQL81Dialect;
|
|||
|
||||
import org.hibernate.testing.RequiresDialect;
|
||||
import org.hibernate.testing.SkipForDialect;
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -83,14 +85,49 @@ public class SearchedCaseExpressionTest extends BaseCoreFunctionalTestCase {
|
|||
|
||||
Assert.assertNotNull(resultList);
|
||||
} );
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class[] getAnnotatedClasses() {
|
||||
return new Class[]{Event.class, EventType.class};
|
||||
}
|
||||
@Test
|
||||
@TestForIssue(jiraKey = "HHH-13167")
|
||||
public void testMissingElseClause() {
|
||||
doInHibernate( this::sessionFactory, session -> {
|
||||
Event event = new Event();
|
||||
event.id = 1L;
|
||||
event.type = EventType.TYPE1;
|
||||
|
||||
@Entity(name = "Event")
|
||||
session.persist( event );
|
||||
} );
|
||||
|
||||
doInHibernate( this::sessionFactory, session -> {
|
||||
CriteriaBuilder cb = session.getCriteriaBuilder();
|
||||
|
||||
CriteriaQuery<Event> criteria = cb.createQuery( Event.class );
|
||||
|
||||
Root<Event> root = criteria.from( Event.class );
|
||||
Path<EventType> type = root.get( "type" );
|
||||
|
||||
Expression<String> caseWhen = cb.<String> selectCase()
|
||||
.when( cb.equal( type, EventType.TYPE1 ), "Matched" );
|
||||
|
||||
criteria.select( root );
|
||||
criteria.where( cb.equal( caseWhen, "Matched" ) );
|
||||
|
||||
Event event = session.createQuery( criteria ).getSingleResult();
|
||||
assertEquals( 1L, (long) event.id );
|
||||
} );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isCleanupTestDataRequired() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class[] getAnnotatedClasses() {
|
||||
return new Class[]{ Event.class, EventType.class };
|
||||
}
|
||||
|
||||
@Entity(name = "Event")
|
||||
public static class Event {
|
||||
|
||||
@Id
|
||||
|
@ -113,7 +150,7 @@ public class SearchedCaseExpressionTest extends BaseCoreFunctionalTestCase {
|
|||
}
|
||||
}
|
||||
|
||||
public enum EventType {
|
||||
public enum EventType {
|
||||
|
||||
TYPE1, TYPE2, TYPE3
|
||||
|
||||
|
|
Loading…
Reference in New Issue