Skip to content

HHH-19572 fix where(List) and having(List) #10398

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public interface JpaQueryStructure<T> extends JpaQueryPart<T> {

JpaQueryStructure<T> setRestriction(Predicate... restrictions);

JpaQueryStructure<T> setRestriction(List<Predicate> restrictions);


// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Grouping (group-by / having) clause
Expand All @@ -78,6 +80,8 @@ public interface JpaQueryStructure<T> extends JpaQueryPart<T> {

JpaQueryStructure<T> setGroupRestriction(Predicate... restrictions);

JpaQueryStructure<T> setGroupRestriction(List<Predicate> restrictions);

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Covariant overrides

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -972,9 +972,11 @@ <T> SqmJsonValueExpression<T> jsonValue(
@Override
SqmPredicate wrap(Expression<Boolean> expression);

@Override
@Override @SuppressWarnings("unchecked")
SqmPredicate wrap(Expression<Boolean>... expressions);

SqmPredicate wrap(List<? extends Expression<Boolean>> restrictions);

@Override
SqmExpression<?> fk(Path<?> path);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,19 @@ public final SqmPredicate wrap(Expression<Boolean>... expressions) {
return new SqmJunctionPredicate( Predicate.BooleanOperator.AND, predicates, this );
}

@Override
public SqmPredicate wrap(List<? extends Expression<Boolean>> restrictions) {
if ( restrictions.size() == 1 ) {
return wrap( restrictions.get( 0 ) );
}

final List<SqmPredicate> predicates = new ArrayList<>( restrictions.size() );
for ( Expression<Boolean> expression : restrictions ) {
predicates.add( wrap( expression ) );
}
return new SqmJunctionPredicate( Predicate.BooleanOperator.AND, predicates, this );
}

@Override @SuppressWarnings("unchecked")
public <T extends HibernateCriteriaBuilder> T unwrap(Class<T> clazz) {
return (T) extensions.get( clazz );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,10 @@ public void setPredicate(SqmPredicate predicate) {

@Override
public void applyPredicate(SqmPredicate predicate) {
if ( this.predicate == null ) {
this.predicate = predicate;
}
else {
this.predicate = nodeBuilder.and( this.predicate, predicate );
}
this.predicate =
this.predicate == null
? predicate
: nodeBuilder.and( this.predicate, predicate );
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,12 @@ public SqmSelectQuery<T> where(Predicate... restrictions) {
return this;
}

@Override
public SqmSelectQuery<T> where(List<Predicate> restrictions) {
getQuerySpec().setRestriction( restrictions );
return this;
}


// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Grouping
Expand All @@ -388,7 +394,7 @@ public List<Expression<?>> getGroupList() {

@Override
public SqmSelectQuery<T> groupBy(Expression<?>... expressions) {
getQuerySpec().setGroupingExpressions( List.of( expressions ) );
getQuerySpec().setGroupingExpressions( expressions );
return this;
}

Expand All @@ -405,13 +411,19 @@ public SqmPredicate getGroupRestriction() {

@Override
public SqmSelectQuery<T> having(Expression<Boolean> booleanExpression) {
getQuerySpec().setGroupRestriction( nodeBuilder().wrap( booleanExpression ) );
getQuerySpec().setGroupRestriction( booleanExpression );
return this;
}

@Override
public SqmSelectQuery<T> having(Predicate... predicates) {
getQuerySpec().setGroupRestriction( nodeBuilder().wrap( predicates ) );
getQuerySpec().setGroupRestriction( predicates );
return this;
}

@Override
public AbstractQuery<T> having(List<Predicate> restrictions) {
getQuerySpec().setGroupRestriction( restrictions );
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,20 +374,44 @@ else if ( restrictions.length == 0 ) {
setWhereClause( null );
}
else {
SqmWhereClause whereClause = getWhereClause();
if ( whereClause == null ) {
setWhereClause( whereClause = new SqmWhereClause( nodeBuilder() ) );
}
else {
whereClause.setPredicate( null );
final SqmWhereClause whereClause = resetWhereClause();
for ( Predicate restriction : restrictions ) {
whereClause.applyPredicate( (SqmPredicate) restriction );
}
}
return this;
}

@Override
public SqmQuerySpec<T> setRestriction(List<Predicate> restrictions) {
if ( restrictions == null ) {
throw new IllegalArgumentException( "The predicate list cannot be null" );
}
else if ( restrictions.isEmpty() ) {
setWhereClause( null );
}
else {
final SqmWhereClause whereClause = resetWhereClause();
for ( Predicate restriction : restrictions ) {
whereClause.applyPredicate( (SqmPredicate) restriction );
}
}
return this;
}

private SqmWhereClause resetWhereClause() {
final SqmWhereClause whereClause = getWhereClause();
if ( whereClause == null ) {
final SqmWhereClause newWhereClause = new SqmWhereClause( nodeBuilder() );
setWhereClause( newWhereClause );
return newWhereClause;
}
else {
whereClause.setPredicate( null );
return whereClause;
}
}

@Override
public List<SqmExpression<?>> getGroupingExpressions() {
return groupByClauseExpressions;
Expand Down Expand Up @@ -442,6 +466,12 @@ public SqmQuerySpec<T> setGroupRestriction(Predicate... restrictions) {
return this;
}

@Override
public SqmQuerySpec<T> setGroupRestriction(List<Predicate> restrictions) {
havingClausePredicate = nodeBuilder().wrap( restrictions );
return this;
}

@Override
public SqmQuerySpec<T> setSortSpecifications(List<? extends JpaOrder> sortSpecifications) {
super.setSortSpecifications( sortSpecifications );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.hibernate.query.sqm.tree.cte.SqmCteStatement;
import org.hibernate.query.sqm.tree.expression.SqmParameter;
import org.hibernate.query.sqm.tree.from.SqmFromClause;
import org.hibernate.query.sqm.tree.predicate.SqmPredicate;
import org.hibernate.query.sqm.tree.from.SqmRoot;

import jakarta.persistence.Tuple;
Expand All @@ -41,7 +40,6 @@
import static java.util.Collections.emptySet;
import static java.util.Collections.unmodifiableList;
import static java.util.Collections.unmodifiableSet;
import static org.hibernate.query.sqm.spi.SqmCreationHelper.combinePredicates;
import static org.hibernate.query.sqm.SqmQuerySource.CRITERIA;
import static org.hibernate.query.sqm.tree.SqmCopyContext.noParamCopyContext;
import static org.hibernate.query.sqm.tree.jpa.ParameterCollector.collectParameters;
Expand Down Expand Up @@ -291,7 +289,8 @@ protected <X> JpaCteCriteria<X> withInternal(

@Override
public SqmSelectStatement<T> distinct(boolean distinct) {
return (SqmSelectStatement<T>) super.distinct( distinct );
super.distinct( distinct );
return this;
}

@Override
Expand All @@ -308,21 +307,6 @@ public <U> SqmSubQuery<U> subquery(EntityType<U> type) {
return new SqmSubQuery<>( this, type, nodeBuilder() );
}

@Override
public SqmSelectStatement<T> where(List<Predicate> restrictions) {
//noinspection rawtypes,unchecked
getQuerySpec().getWhereClause().applyPredicates( (List) restrictions );
return this;
}

@Override
public SqmSelectStatement<T> having(List<Predicate> restrictions) {
final SqmPredicate combined =
combinePredicates( getQuerySpec().getHavingClausePredicate(), restrictions );
getQuerySpec().setHavingClausePredicate( combined );
return this;
}

@Override
@SuppressWarnings("unchecked")
public SqmSelectStatement<T> select(Selection<? extends T> selection) {
Expand Down Expand Up @@ -413,32 +397,50 @@ public <U> SqmSubQuery<U> subquery(Class<U> type) {

@Override
public SqmSelectStatement<T> where(Expression<Boolean> restriction) {
return (SqmSelectStatement<T>) super.where( restriction );
super.where( restriction );
return this;
}

@Override
public SqmSelectStatement<T> where(Predicate... restrictions) {
return (SqmSelectStatement<T>) super.where( restrictions );
super.where( restrictions );
return this;
}

@Override
public SqmSelectStatement<T> where(List<Predicate> restrictions) {
super.where( restrictions );
return this;
}

@Override
public SqmSelectStatement<T> groupBy(Expression<?>... expressions) {
return (SqmSelectStatement<T>) super.groupBy( expressions );
super.groupBy( expressions );
return this;
}

@Override
public SqmSelectStatement<T> groupBy(List<Expression<?>> grouping) {
return (SqmSelectStatement<T>) super.groupBy( grouping );
super.groupBy( grouping );
return this;
}

@Override
public SqmSelectStatement<T> having(Expression<Boolean> booleanExpression) {
return (SqmSelectStatement<T>) super.having( booleanExpression );
super.having( booleanExpression );
return this;
}

@Override
public SqmSelectStatement<T> having(Predicate... predicates) {
return (SqmSelectStatement<T>) super.having( predicates );
super.having( predicates );
return this;
}

@Override
public SqmSelectStatement<T> having(List<Predicate> restrictions) {
super.having( restrictions );
return this;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@
import jakarta.persistence.metamodel.EntityType;

import static java.util.Collections.emptySet;
import static org.hibernate.query.sqm.spi.SqmCreationHelper.combinePredicates;

/**
* @author Steve Ebersole
Expand Down Expand Up @@ -345,37 +344,56 @@ public List<Selection<?>> getCompoundSelectionItems() {

@Override
public SqmSubQuery<T> distinct(boolean distinct) {
return (SqmSubQuery<T>) super.distinct( distinct );
super.distinct( distinct );
return this;
}

@Override
public SqmSubQuery<T> where(Expression<Boolean> restriction) {
return (SqmSubQuery<T>) super.where( restriction );
super.where( restriction );
return this;
}

@Override
public SqmSubQuery<T> where(Predicate... restrictions) {
return (SqmSubQuery<T>) super.where( restrictions );
super.where( restrictions );
return this;
}

@Override
public SqmSubQuery<T> where(List<Predicate> restrictions) {
super.where( restrictions );
return this;
}

@Override
public SqmSubQuery<T> groupBy(Expression<?>... expressions) {
return (SqmSubQuery<T>) super.groupBy( expressions );
super.groupBy( expressions );
return this;
}

@Override
public SqmSubQuery<T> groupBy(List<Expression<?>> grouping) {
return (SqmSubQuery<T>) super.groupBy( grouping );
super.groupBy( grouping );
return this;
}

@Override
public SqmSubQuery<T> having(Expression<Boolean> booleanExpression) {
return (SqmSubQuery<T>) super.having( booleanExpression );
super.having( booleanExpression );
return this;
}

@Override
public SqmSubQuery<T> having(Predicate... predicates) {
return (SqmSubQuery<T>) super.having( predicates );
super.having( predicates );
return this;
}

@Override
public SqmSubQuery<T> having(List<Predicate> restrictions) {
super.having( restrictions );
return this;
}

@Override
Expand Down Expand Up @@ -721,21 +739,6 @@ public <U> Subquery<U> subquery(EntityType<U> type) {
return new SqmSubQuery<>( this, type, nodeBuilder() );
}

@Override
public Subquery<T> where(List<Predicate> restrictions) {
//noinspection rawtypes,unchecked
getQuerySpec().getWhereClause().applyPredicates( (List) restrictions );
return this;
}

@Override
public Subquery<T> having(List<Predicate> restrictions) {
final SqmPredicate combined =
combinePredicates( getQuerySpec().getHavingClausePredicate(), restrictions );
getQuerySpec().setHavingClausePredicate( combined );
return this;
}

@Override
public Set<ParameterExpression<?>> getParameters() {
return emptySet();
Expand Down
Loading