Skip to content

Commit 6dce2d1

Browse files
committed
HHH-18979 improved approach to multiple Restrictions
1 parent 50d5c0c commit 6dce2d1

File tree

9 files changed

+23
-66
lines changed

9 files changed

+23
-66
lines changed

hibernate-core/src/main/java/org/hibernate/query/Query.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -930,9 +930,6 @@ default Query<R> setPage(Page page) {
930930
@Override @Incubating
931931
Query<R> addRestriction(Restriction<? super R> restriction);
932932

933-
@Override @Incubating
934-
Query<R> addRestrictions(List<Restriction<? super R>> restrictionList);
935-
936933
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
937934
// deprecated methods
938935

hibernate-core/src/main/java/org/hibernate/query/Restriction.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import org.hibernate.Internal;
1313
import org.hibernate.query.range.Range;
1414

15+
import java.util.List;
16+
1517
/**
1618
* A rule for restricting query results.
1719
* <p>
@@ -158,14 +160,22 @@ static <T> Restriction<T> notContains(SingularAttribute<T, String> attribute, St
158160
return contains( attribute, substring ).negated();
159161
}
160162

163+
static <T> Restriction<T> and(List<? extends Restriction<? super T>> restrictions) {
164+
return new Conjunction<>( restrictions );
165+
}
166+
167+
static <T> Restriction<T> or(List<? extends Restriction<? super T>> restrictions) {
168+
return new Disjunction<>( restrictions );
169+
}
170+
161171
@SafeVarargs
162-
static <T> Restriction<T> and(Restriction<T>... restrictions) {
163-
return new Conjunction<>( java.util.List.of( restrictions ) );
172+
static <T> Restriction<T> and(Restriction<? super T>... restrictions) {
173+
return new Conjunction<T>( java.util.List.of( restrictions ) );
164174
}
165175

166176
@SafeVarargs
167-
static <T> Restriction<T> or(Restriction<T>... restrictions) {
168-
return new Disjunction<>( java.util.List.of( restrictions ) );
177+
static <T> Restriction<T> or(Restriction<? super T>... restrictions) {
178+
return new Disjunction<T>( java.util.List.of( restrictions ) );
169179
}
170180

171181
static <T> Restriction<T> unrestricted() {

hibernate-core/src/main/java/org/hibernate/query/SelectionQuery.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -618,19 +618,6 @@ default Stream<R> stream() {
618618
@Incubating
619619
SelectionQuery<R> addRestriction(Restriction<? super R> restriction);
620620

621-
/**
622-
* If the result type of this query is an entity class, add a
623-
* {@linkplain Restriction rule} for restricting the query results.
624-
*
625-
* @param restrictionList a list of {@link Restriction}s
626-
*
627-
* @see Restriction
628-
*
629-
* @since 7.0
630-
*/
631-
@Incubating
632-
SelectionQuery<R> addRestrictions(List<Restriction<? super R>> restrictionList);
633-
634621
/**
635622
* Specifies whether follow-on locking should be applied
636623
*/

hibernate-core/src/main/java/org/hibernate/query/spi/AbstractQuery.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -310,11 +310,6 @@ public Query<R> addRestriction(Restriction<? super R> restriction) {
310310
throw new UnsupportedOperationException( "Should be implemented by " + this.getClass().getName() );
311311
}
312312

313-
@Override
314-
public Query<R> addRestrictions(List<Restriction<? super R>> restrictionList) {
315-
throw new UnsupportedOperationException( "Should be implemented by " + this.getClass().getName() );
316-
}
317-
318313
@Override
319314
public String getComment() {
320315
return super.getComment();

hibernate-core/src/main/java/org/hibernate/query/sql/internal/NativeQueryImpl.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1674,11 +1674,6 @@ public Query<R> addRestriction(Restriction<? super R> restriction) {
16741674
throw new UnsupportedOperationException("Restrictions not currently supported for native queries");
16751675
}
16761676

1677-
@Override
1678-
public Query<R> addRestrictions(List<Restriction<? super R>> restrictionList) {
1679-
throw new UnsupportedOperationException("Restrictions not currently supported for native queries");
1680-
}
1681-
16821677
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16831678
// Hints
16841679

hibernate-core/src/main/java/org/hibernate/query/sqm/internal/AbstractSqmSelectionQuery.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*/
55
package org.hibernate.query.sqm.internal;
66

7-
import jakarta.persistence.criteria.Predicate;
87
import jakarta.persistence.criteria.Root;
98
import org.hibernate.HibernateException;
109
import org.hibernate.engine.spi.SharedSessionContractImplementor;
@@ -40,7 +39,6 @@
4039
import org.hibernate.type.BasicType;
4140
import org.hibernate.type.BasicTypeRegistry;
4241

43-
import java.util.ArrayList;
4442
import java.util.List;
4543
import java.util.Map;
4644

@@ -162,23 +160,6 @@ public SelectionQuery<R> addRestriction(Restriction<? super R> restriction) {
162160
return this;
163161
}
164162

165-
@Override
166-
public SelectionQuery<R> addRestrictions(List<Restriction<? super R>> restrictionList) {
167-
final SqmSelectStatement<R> selectStatement = getSqmSelectStatement().copy( noParamCopyContext() );
168-
final Root<? extends R> root = (Root<? extends R>) selectStatement.getRootList().get( 0 );
169-
final List<Predicate> list = new ArrayList<>( restrictionList.size() );
170-
list.add( selectStatement.getRestriction() );
171-
for ( var restriction : restrictionList ) {
172-
list.add( restriction.toPredicate( root, selectStatement.nodeBuilder() ) );
173-
}
174-
selectStatement.where( list.toArray(new Predicate[0]) );
175-
// TODO: when the QueryInterpretationCache can handle caching criteria queries,
176-
// simply cache the new SQM as if it were a criteria query, and remove this:
177-
getQueryOptions().setQueryPlanCachingEnabled( false );
178-
setSqmStatement( selectStatement );
179-
return this;
180-
}
181-
182163
@Override
183164
public SelectionQuery<R> setPage(Page page) {
184165
setMaxResults( page.getMaxResults() );

hibernate-core/src/main/java/org/hibernate/query/sqm/internal/QuerySqmImpl.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -764,12 +764,6 @@ public Query<R> addRestriction(Restriction<? super R> restriction) {
764764
return this;
765765
}
766766

767-
@Override
768-
public Query<R> addRestrictions(List<Restriction<? super R>> restrictionList) {
769-
super.addRestrictions( restrictionList );
770-
return this;
771-
}
772-
773767
@Override
774768
public Query<R> setOrder(List<? extends Order<? super R>> orders) {
775769
super.setOrder(orders);

tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AbstractQueryMethod.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -294,20 +294,14 @@ void handleRestrictionParameters(
294294
final String paramName = paramNames.get(i);
295295
final String paramType = paramTypes.get(i);
296296
if ( isRestrictionParam(paramType) ) {
297-
if ( paramType.startsWith(LIST) ) {
297+
if ( paramType.startsWith(LIST) || paramType.endsWith("[]") ) {
298298
declaration
299-
.append( "\t\t\t.addRestrictions(" )
300-
.append( paramName )
301-
.append( ")\n" );
302-
303-
}
304-
else if ( paramType.endsWith("[]") ) {
305-
declaration
306-
.append( "\t\t\t.addRestrictions(" )
307-
.append( annotationMetaEntity.importType(LIST) )
308-
.append( ".of(" )
299+
.append( "\t\t\t.addRestriction(" )
300+
.append( annotationMetaEntity.importType(HIB_RESTRICTION) )
301+
.append( ".and(" )
309302
.append( paramName )
310303
.append( "))\n" );
304+
311305
}
312306
else {
313307
declaration

tooling/metamodel-generator/src/test/java/org/hibernate/processor/test/restriction/Bookshelf.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.hibernate.annotations.processing.HQL;
1010
import org.hibernate.query.Order;
1111
import org.hibernate.query.Restriction;
12+
import org.hibernate.query.range.Range;
1213

1314
import java.util.List;
1415

@@ -36,4 +37,7 @@ public interface Bookshelf {
3637
List<Book> books4(Order<? super Book>... orders);
3738
@HQL("from Book")
3839
List<Book> book5(List<Restriction<? super Book>> restrictions, List<Order<? super Book>> orders);
40+
41+
@Find Book[] books(Range<String> isbn);
42+
@Find List<Book> booksByTitle(Range<String> title);
3943
}

0 commit comments

Comments
 (0)