HHH-13167 - When omitting the OTHERWISE clause in a CASE expression built with Criteria API, Hibernate throws a NullPointerException

This commit is contained in:
Vlad Mihalcea 2018-12-19 11:27:41 +02:00 committed by Guillaume Smet
parent fa5b632f4e
commit 33efabd597
2 changed files with 52 additions and 10 deletions

View File

@ -114,9 +114,14 @@ public class SearchedCaseExpression<R>
.append( ( (Renderable) whenClause.getResult() ).render( renderingContext ) ); .append( ( (Renderable) whenClause.getResult() ).render( renderingContext ) );
} }
Expression<?> otherwiseResult = getOtherwiseResult();
if ( otherwiseResult != null ) {
caseStatement.append( " else " ) caseStatement.append( " else " )
.append( ( (Renderable) getOtherwiseResult() ).render( renderingContext ) ) .append( ( (Renderable) otherwiseResult ).render( renderingContext ) );
.append( " end" ); }
caseStatement.append( " end" );
return caseStatement.toString(); return caseStatement.toString();
} }

View File

@ -24,11 +24,13 @@ import org.hibernate.dialect.PostgreSQL81Dialect;
import org.hibernate.testing.RequiresDialect; import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.SkipForDialect; import org.hibernate.testing.SkipForDialect;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertEquals;
/** /**
* *
@ -85,9 +87,44 @@ public class SearchedCaseExpressionTest extends BaseCoreFunctionalTestCase {
} ); } );
} }
@Test
@TestForIssue(jiraKey = "HHH-13167")
public void testMissingElseClause() {
doInHibernate( this::sessionFactory, session -> {
Event event = new Event();
event.id = 1L;
event.type = EventType.TYPE1;
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 @Override
protected Class[] getAnnotatedClasses() { protected Class[] getAnnotatedClasses() {
return new Class[]{Event.class, EventType.class}; return new Class[]{ Event.class, EventType.class };
} }
@Entity(name = "Event") @Entity(name = "Event")