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 @Override
public SqmQuerySpec<T> setRestriction(Predicate... restrictions) { 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(); SqmWhereClause whereClause = getWhereClause();
if ( whereClause == null ) { if ( whereClause == null ) {
setWhereClause( whereClause = new SqmWhereClause( nodeBuilder() ) ); setWhereClause( whereClause = new SqmWhereClause( nodeBuilder() ) );
@ -364,6 +371,7 @@ public class SqmQuerySpec<T> extends SqmQueryPart<T>
for ( Predicate restriction : restrictions ) { for ( Predicate restriction : restrictions ) {
whereClause.applyPredicate( (SqmPredicate) restriction ); whereClause.applyPredicate( (SqmPredicate) restriction );
} }
}
return this; return this;
} }
@ -632,7 +640,7 @@ public class SqmQuerySpec<T> extends SqmQueryPart<T>
if ( sqmJoin instanceof SqmAttributeJoin<?, ?> ) { if ( sqmJoin instanceof SqmAttributeJoin<?, ?> ) {
final SqmAttributeJoin<?, ?> attributeJoin = (SqmAttributeJoin<?, ?>) sqmJoin; final SqmAttributeJoin<?, ?> attributeJoin = (SqmAttributeJoin<?, ?>) sqmJoin;
sb.append( sqmFrom.resolveAlias() ).append( '.' ); sb.append( sqmFrom.resolveAlias() ).append( '.' );
sb.append( (attributeJoin).getAttribute().getName() ); sb.append( ( attributeJoin ).getAttribute().getName() );
sb.append( ' ' ).append( sqmJoin.resolveAlias() ); sb.append( ' ' ).append( sqmJoin.resolveAlias() );
if ( attributeJoin.getJoinPredicate() != null ) { if ( attributeJoin.getJoinPredicate() != null ) {
sb.append( " on " ); sb.append( " on " );
@ -647,7 +655,7 @@ public class SqmQuerySpec<T> extends SqmQueryPart<T>
} }
else if ( sqmJoin instanceof SqmEntityJoin<?> ) { else if ( sqmJoin instanceof SqmEntityJoin<?> ) {
final SqmEntityJoin<?> sqmEntityJoin = (SqmEntityJoin<?>) sqmJoin; final SqmEntityJoin<?> sqmEntityJoin = (SqmEntityJoin<?>) sqmJoin;
sb.append( (sqmEntityJoin).getEntityName() ); sb.append( ( sqmEntityJoin ).getEntityName() );
sb.append( ' ' ).append( sqmJoin.resolveAlias() ); sb.append( ' ' ).append( sqmJoin.resolveAlias() );
if ( sqmEntityJoin.getJoinPredicate() != null ) { if ( sqmEntityJoin.getJoinPredicate() != null ) {
sb.append( " on " ); sb.append( " on " );

View File

@ -48,7 +48,8 @@ public class EmptyPredicateTest {
final CriteriaBuilder cb = session.getCriteriaBuilder(); final CriteriaBuilder cb = session.getCriteriaBuilder();
final CriteriaQuery<BasicEntity> query = cb.createQuery( BasicEntity.class ); final CriteriaQuery<BasicEntity> query = cb.createQuery( BasicEntity.class );
final Root<BasicEntity> root = query.from( 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() ); assertEquals( 1, session.createQuery( query ).getResultList().size() );
} ); } );
} }