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

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() );
} );
}