HHH-15901 Check for null predicates in where clause

This commit is contained in:
Marco Belladelli 2022-12-22 11:58:24 +01:00 committed by Christian Beikov
parent 845adf36b9
commit cbfba13143
2 changed files with 19 additions and 10 deletions

View File

@ -354,6 +354,13 @@ public class SqmQuerySpec<T> extends SqmQueryPart<T>
@Override
public SqmQuerySpec<T> setRestriction(Predicate... restrictions) {
if ( restrictions == null ) {
throw new IllegalArgumentException( "The predicate array cannot be null" );
}
else if ( restrictions.length == 0 ) {
setWhereClause( null );
}
else {
SqmWhereClause whereClause = getWhereClause();
if ( whereClause == null ) {
setWhereClause( whereClause = new SqmWhereClause( nodeBuilder() ) );
@ -364,6 +371,7 @@ public class SqmQuerySpec<T> extends SqmQueryPart<T>
for ( Predicate restriction : restrictions ) {
whereClause.applyPredicate( (SqmPredicate) restriction );
}
}
return this;
}

View File

@ -48,7 +48,8 @@ public class EmptyPredicateTest {
final CriteriaBuilder cb = session.getCriteriaBuilder();
final CriteriaQuery<BasicEntity> query = cb.createQuery( BasicEntity.class );
final Root<BasicEntity> root = query.from( BasicEntity.class );
query.select( root ).where( new Predicate[] {} );
query.select( root ).where( cb.equal( cb.literal( 1 ), 2 ) );
query.where( new Predicate[] {} ); // this should remove previous restrictions
assertEquals( 1, session.createQuery( query ).getResultList().size() );
} );
}