Skip to content

Commit 50d5c0c

Browse files
committed
HHH-18979 handle vararg and List of Restriction and Order
1 parent a71bc5b commit 50d5c0c

File tree

11 files changed

+158
-12
lines changed

11 files changed

+158
-12
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -927,9 +927,12 @@ default Query<R> setPage(Page page) {
927927
@Override @Incubating
928928
Query<R> setOrder(Order<? super R> order);
929929

930-
@Override
930+
@Override @Incubating
931931
Query<R> addRestriction(Restriction<? super R> restriction);
932932

933+
@Override @Incubating
934+
Query<R> addRestrictions(List<Restriction<? super R>> restrictionList);
935+
933936
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
934937
// deprecated methods
935938

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,19 @@ 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+
621634
/**
622635
* Specifies whether follow-on locking should be applied
623636
*/

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,11 @@ 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+
313318
@Override
314319
public String getComment() {
315320
return super.getComment();

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1671,7 +1671,12 @@ public Query<R> setOrder(Order<? super R> order) {
16711671

16721672
@Override
16731673
public Query<R> addRestriction(Restriction<? super R> restriction) {
1674-
throw new UnsupportedOperationException("Ordering not currently supported for native queries");
1674+
throw new UnsupportedOperationException("Restrictions not currently supported for native queries");
1675+
}
1676+
1677+
@Override
1678+
public Query<R> addRestrictions(List<Restriction<? super R>> restrictionList) {
1679+
throw new UnsupportedOperationException("Restrictions not currently supported for native queries");
16751680
}
16761681

16771682
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

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

7+
import jakarta.persistence.criteria.Predicate;
78
import jakarta.persistence.criteria.Root;
89
import org.hibernate.HibernateException;
910
import org.hibernate.engine.spi.SharedSessionContractImplementor;
@@ -39,6 +40,7 @@
3940
import org.hibernate.type.BasicType;
4041
import org.hibernate.type.BasicTypeRegistry;
4142

43+
import java.util.ArrayList;
4244
import java.util.List;
4345
import java.util.Map;
4446

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

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+
163182
@Override
164183
public SelectionQuery<R> setPage(Page page) {
165184
setMaxResults( page.getMaxResults() );

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,12 @@ 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+
767773
@Override
768774
public Query<R> setOrder(List<? extends Order<? super R>> orders) {
769775
super.setOrder(orders);

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

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -294,10 +294,27 @@ void handleRestrictionParameters(
294294
final String paramName = paramNames.get(i);
295295
final String paramType = paramTypes.get(i);
296296
if ( isRestrictionParam(paramType) ) {
297-
declaration
298-
.append("\t\t\t.addRestriction(")
299-
.append(paramName)
300-
.append(")\n");
297+
if ( paramType.startsWith(LIST) ) {
298+
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(" )
309+
.append( paramName )
310+
.append( "))\n" );
311+
}
312+
else {
313+
declaration
314+
.append( "\t\t\t.addRestriction(" )
315+
.append( paramName )
316+
.append( ")\n" );
317+
}
301318
}
302319
else if ( isRangeParam(paramType) ) {
303320
declaration
@@ -378,11 +395,12 @@ static boolean isOrderParam(String parameterType) {
378395
return parameterType.startsWith(HIB_ORDER)
379396
|| parameterType.startsWith(LIST + "<" + HIB_ORDER)
380397
|| parameterType.startsWith(JD_SORT)
381-
|| parameterType.startsWith(JD_ORDER);
398+
|| parameterType.startsWith(JD_ORDER) && !parameterType.endsWith("[]");
382399
}
383400

384401
static boolean isRestrictionParam(String parameterType) {
385-
return parameterType.startsWith(HIB_RESTRICTION);
402+
return parameterType.startsWith(HIB_RESTRICTION)
403+
|| parameterType.startsWith(LIST + "<" + HIB_RESTRICTION);
386404
}
387405

388406
static boolean isRangeParam(String parameterType) {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,7 +1590,7 @@ else if ( returnType.getKind() == TypeKind.DECLARED ) {
15901590
}
15911591
else {
15921592
for ( VariableElement parameter : method.getParameters() ) {
1593-
final String type = typeName(parameter.asType());
1593+
final String type = parameter.asType().toString();
15941594
if ( isPageParam(type) ) {
15951595
message( parameter, "pagination would have no effect", Diagnostic.Kind.ERROR);
15961596
}
@@ -1671,7 +1671,7 @@ private void createCriteriaFinder(
16711671
private void checkFinderParameter(TypeElement entity, VariableElement parameter) {
16721672
final Types types = context.getTypeUtils();
16731673
final TypeMirror parameterType = parameterType(parameter);
1674-
final String typeName = typeName( parameterType );
1674+
final String typeName = parameterType.toString();
16751675
if ( isOrderParam( typeName ) || isRestrictionParam( typeName ) ) {
16761676
final TypeMirror typeArgument = getTypeArgument( parameterType, entity );
16771677
if ( typeArgument == null ) {
@@ -1819,8 +1819,8 @@ private OrderBy orderByExpression(AnnotationMirror orderBy, TypeElement entityTy
18191819
}
18201820
}
18211821

1822-
private static boolean isFinderParameterMappingToAttribute(VariableElement param) {
1823-
final String typeName = typeName( param.asType() );
1822+
private static boolean isFinderParameterMappingToAttribute(VariableElement parameter) {
1823+
final String typeName = parameter.asType().toString();
18241824
return !isSpecialParam(typeName)
18251825
|| isRangeParam(typeName);
18261826
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.processor.test.restriction;
6+
7+
import jakarta.persistence.Entity;
8+
import jakarta.persistence.Id;
9+
10+
@Entity
11+
public class Book {
12+
@Id
13+
String isbn;
14+
String title;
15+
String author;
16+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.processor.test.restriction;
6+
7+
import org.hibernate.StatelessSession;
8+
import org.hibernate.annotations.processing.Find;
9+
import org.hibernate.annotations.processing.HQL;
10+
import org.hibernate.query.Order;
11+
import org.hibernate.query.Restriction;
12+
13+
import java.util.List;
14+
15+
16+
public interface Bookshelf {
17+
StatelessSession session();
18+
@Find Book book(String isbn);
19+
@Find
20+
List<Book> books(Restriction<? super Book> restriction);
21+
@Find
22+
List<Book> books(Restriction<? super Book>... restrictions);
23+
@Find
24+
List<Book> books(Order<? super Book> order);
25+
@Find
26+
List<Book> books(Order<? super Book>... orders);
27+
@Find
28+
List<Book> books(List<Restriction<? super Book>> restrictions, List<Order<? super Book>> orders);
29+
@HQL("from Book")
30+
List<Book> books1(Restriction<? super Book> restriction);
31+
@HQL("from Book")
32+
List<Book> books2(Restriction<? super Book>... restrictions);
33+
@HQL("from Book")
34+
List<Book> books3(Order<? super Book> order);
35+
@HQL("from Book")
36+
List<Book> books4(Order<? super Book>... orders);
37+
@HQL("from Book")
38+
List<Book> book5(List<Restriction<? super Book>> restrictions, List<Order<? super Book>> orders);
39+
}

0 commit comments

Comments
 (0)