Skip to content

Commit bb0be44

Browse files
sebersolegavinking
authored andcommitted
HHH-19382 - Drop incubating SelectionQuery#setOrder methods
1 parent cb7b02b commit bb0be44

25 files changed

+319
-380
lines changed

hibernate-core/src/main/java/org/hibernate/procedure/internal/ProcedureCallImpl.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import org.hibernate.query.BindableType;
4545
import org.hibernate.query.KeyedPage;
4646
import org.hibernate.query.KeyedResultList;
47-
import org.hibernate.query.Order;
4847
import org.hibernate.query.OutputableType;
4948
import org.hibernate.query.Query;
5049
import org.hibernate.query.QueryParameter;
@@ -411,16 +410,6 @@ public QueryParameterBindings getParameterBindings() {
411410
return parameterBindings;
412411
}
413412

414-
@Override
415-
public Query<R> setOrder(List<? extends Order<? super R>> orderList) {
416-
throw new UnsupportedOperationException("Ordering not supported for stored procedure calls");
417-
}
418-
419-
@Override
420-
public Query<R> setOrder(Order<? super R> order) {
421-
throw new UnsupportedOperationException("Ordering not supported for stored procedure calls");
422-
}
423-
424413

425414
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
426415
// Parameter registrations

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* <p>
2020
* This is a convenience class which allows query result ordering rules to be
2121
* passed around the system before being applied to a {@link Query} by calling
22-
* {@link org.hibernate.query.programmatic.SelectionSpecification#sort(Order)}.
22+
* {@link org.hibernate.query.specification.SelectionSpecification#sort(Order)}.
2323
* <pre>
2424
* SelectionSpecification.create(Book.class,
2525
* "from Book b join b.authors a where a.name = :name")
@@ -30,7 +30,7 @@
3030
* </pre>
3131
* <p>
3232
* {@code Order}s may be stacked using {@link List#of} and
33-
* {@link org.hibernate.query.programmatic.SelectionSpecification#resort(List)}.
33+
* {@link org.hibernate.query.specification.SelectionSpecification#resort(List)}.
3434
* <pre>
3535
* SelectionSpecification.create(Book.class,
3636
* "from Book b join b.authors a where a.name = :name")
@@ -51,8 +51,8 @@
5151
* used by Hibernate Data Repositories to implement Jakarta Data
5252
* query methods.
5353
*
54-
* @see org.hibernate.query.programmatic.SelectionSpecification#sort(Order)
55-
* @see org.hibernate.query.programmatic.SelectionSpecification#resort(List)
54+
* @see org.hibernate.query.specification.SelectionSpecification#sort(Order)
55+
* @see org.hibernate.query.specification.SelectionSpecification#resort(List)
5656
* @see org.hibernate.query.restriction.Restriction
5757
*
5858
* @author Gavin King

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -928,11 +928,6 @@ default Query<R> setPage(Page page) {
928928
@Override
929929
Query<R> setLockMode(LockModeType lockMode);
930930

931-
@Override @Incubating
932-
Query<R> setOrder(List<? extends Order<? super R>> orderList);
933-
934-
@Override @Incubating
935-
Query<R> setOrder(Order<? super R> order);
936931

937932
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
938933
// deprecated methods

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

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -596,32 +596,6 @@ default Stream<R> stream() {
596596
*/
597597
SelectionQuery<R> setLockMode(String alias, LockMode lockMode);
598598

599-
/**
600-
* If the result type of this query is an entity class, add one or more
601-
* {@linkplain Order rules} for ordering the query results.
602-
*
603-
* @param orderList one or more instances of {@link Order}
604-
*
605-
* @see Order
606-
*
607-
* @since 6.3
608-
*/
609-
@Incubating
610-
SelectionQuery<R> setOrder(List<? extends Order<? super R>> orderList);
611-
612-
/**
613-
* If the result type of this query is an entity class, add a
614-
* {@linkplain Order rule} for ordering the query results.
615-
*
616-
* @param order an instance of {@link Order}
617-
*
618-
* @see Order
619-
*
620-
* @since 6.3
621-
*/
622-
@Incubating
623-
SelectionQuery<R> setOrder(Order<? super R> order);
624-
625599
/**
626600
* Specifies whether follow-on locking should be applied
627601
*/

hibernate-core/src/main/java/org/hibernate/query/programmatic/package-info.java

Lines changed: 0 additions & 12 deletions
This file was deleted.

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919

2020
/**
2121
* A rule for restricting query results. This allows restrictions to be added to
22-
* a {@link org.hibernate.query.programmatic.SelectionSpecification} by calling
23-
* {@link org.hibernate.query.programmatic.SelectionSpecification#restrict(Restriction)}.
22+
* a {@link org.hibernate.query.specification.SelectionSpecification} by calling
23+
* {@link org.hibernate.query.specification.SelectionSpecification#restrict(Restriction)}.
2424
* <pre>
2525
* SelectionSpecification.create(Book.class)
2626
* .restrict(Restriction.like(Book_.title, "%Hibernate%", false))
@@ -47,9 +47,9 @@
4747
* is used by Hibernate Data Repositories to implement Jakarta Data
4848
* query methods.
4949
*
50-
* @see org.hibernate.query.programmatic.SelectionSpecification
51-
* @see org.hibernate.query.programmatic.MutationSpecification
52-
* @see org.hibernate.query.programmatic.QuerySpecification#restrict(Restriction)
50+
* @see org.hibernate.query.specification.SelectionSpecification
51+
* @see org.hibernate.query.specification.MutationSpecification
52+
* @see org.hibernate.query.specification.QuerySpecification#restrict(Restriction)
5353
*
5454
* @see Path
5555
* @see Order

hibernate-core/src/main/java/org/hibernate/query/programmatic/MutationSpecification.java renamed to hibernate-core/src/main/java/org/hibernate/query/specification/MutationSpecification.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* SPDX-License-Identifier: Apache-2.0
33
* Copyright Red Hat Inc. and Hibernate Authors
44
*/
5-
package org.hibernate.query.programmatic;
5+
package org.hibernate.query.specification;
66

77
import jakarta.persistence.criteria.CommonAbstractCriteria;
88
import jakarta.persistence.criteria.CriteriaUpdate;
@@ -14,7 +14,7 @@
1414
import org.hibernate.StatelessSession;
1515
import org.hibernate.query.IllegalMutationQueryException;
1616
import org.hibernate.query.MutationQuery;
17-
import org.hibernate.query.programmatic.internal.MutationSpecificationImpl;
17+
import org.hibernate.query.specification.internal.MutationSpecificationImpl;
1818
import org.hibernate.query.restriction.Restriction;
1919

2020
/**

hibernate-core/src/main/java/org/hibernate/query/programmatic/QuerySpecification.java renamed to hibernate-core/src/main/java/org/hibernate/query/specification/QuerySpecification.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* SPDX-License-Identifier: Apache-2.0
33
* Copyright Red Hat Inc. and Hibernate Authors
44
*/
5-
package org.hibernate.query.programmatic;
5+
package org.hibernate.query.specification;
66

77
import jakarta.persistence.EntityManager;
88
import jakarta.persistence.criteria.CriteriaBuilder;

hibernate-core/src/main/java/org/hibernate/query/programmatic/SelectionSpecification.java renamed to hibernate-core/src/main/java/org/hibernate/query/specification/SelectionSpecification.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* SPDX-License-Identifier: Apache-2.0
33
* Copyright Red Hat Inc. and Hibernate Authors
44
*/
5-
package org.hibernate.query.programmatic;
5+
package org.hibernate.query.specification;
66

77
import jakarta.persistence.EntityManager;
88
import jakarta.persistence.criteria.CriteriaQuery;
@@ -15,7 +15,7 @@
1515
import org.hibernate.query.IllegalSelectQueryException;
1616
import org.hibernate.query.Order;
1717
import org.hibernate.query.SelectionQuery;
18-
import org.hibernate.query.programmatic.internal.SelectionSpecificationImpl;
18+
import org.hibernate.query.specification.internal.SelectionSpecificationImpl;
1919
import org.hibernate.query.restriction.Path;
2020
import org.hibernate.query.restriction.Restriction;
2121

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* SPDX-License-Identifier: Apache-2.0
33
* Copyright Red Hat Inc. and Hibernate Authors
44
*/
5-
package org.hibernate.query.programmatic.internal;
5+
package org.hibernate.query.specification.internal;
66

77
import jakarta.persistence.EntityManager;
88
import jakarta.persistence.criteria.CriteriaBuilder;
@@ -15,7 +15,7 @@
1515
import org.hibernate.SharedSessionContract;
1616
import org.hibernate.StatelessSession;
1717
import org.hibernate.engine.spi.SharedSessionContractImplementor;
18-
import org.hibernate.query.programmatic.MutationSpecification;
18+
import org.hibernate.query.specification.MutationSpecification;
1919
import org.hibernate.query.IllegalMutationQueryException;
2020
import org.hibernate.query.MutationQuery;
2121
import org.hibernate.query.restriction.Restriction;
Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* SPDX-License-Identifier: Apache-2.0
33
* Copyright Red Hat Inc. and Hibernate Authors
44
*/
5-
package org.hibernate.query.programmatic.internal;
5+
package org.hibernate.query.specification.internal;
66

77
import jakarta.persistence.EntityManager;
88
import jakarta.persistence.criteria.CriteriaQuery;
@@ -13,10 +13,11 @@
1313
import org.hibernate.SharedSessionContract;
1414
import org.hibernate.StatelessSession;
1515
import org.hibernate.engine.spi.SharedSessionContractImplementor;
16+
import org.hibernate.internal.util.collections.CollectionHelper;
1617
import org.hibernate.query.IllegalSelectQueryException;
1718
import org.hibernate.query.Order;
1819
import org.hibernate.query.SelectionQuery;
19-
import org.hibernate.query.programmatic.SelectionSpecification;
20+
import org.hibernate.query.specification.SelectionSpecification;
2021
import org.hibernate.query.restriction.Path;
2122
import org.hibernate.query.restriction.Restriction;
2223
import org.hibernate.query.spi.HqlInterpretation;
@@ -28,8 +29,11 @@
2829
import org.hibernate.query.sqm.tree.from.SqmRoot;
2930
import org.hibernate.query.sqm.tree.predicate.SqmPredicate;
3031
import org.hibernate.query.sqm.tree.select.SqmOrderByClause;
32+
import org.hibernate.query.sqm.tree.select.SqmSelectClause;
3133
import org.hibernate.query.sqm.tree.select.SqmSelectStatement;
34+
import org.hibernate.query.sqm.tree.select.SqmSelection;
3235
import org.hibernate.query.sqm.tree.select.SqmSortSpecification;
36+
import org.hibernate.type.descriptor.java.JavaType;
3337

3438
import java.util.ArrayList;
3539
import java.util.List;
@@ -159,8 +163,8 @@ else if ( criteriaQuery != null ) {
159163
var query = builder.createQuery( resultType );
160164
var root = query.from( resultType );
161165
query.select( root );
162-
sqmRoot = (SqmRoot<T>) root;
163-
sqmStatement = (SqmSelectStatement<T>) query;
166+
sqmRoot = root;
167+
sqmStatement = query;
164168
}
165169
specifications.forEach( consumer -> consumer.accept( sqmStatement, sqmRoot ) );
166170
return sqmStatement;
@@ -222,7 +226,17 @@ private SqmRoot<T> extractRoot(SqmSelectStatement<T> sqmStatement, Class<T> resu
222226
if ( sqmRoots.size() > 1 ) {
223227
throw new QueryException( "Query defined multiple roots", hql );
224228
}
229+
225230
final SqmRoot<?> sqmRoot = sqmRoots.iterator().next();
231+
validateRoot( sqmRoot, resultType, hql );
232+
// for later, to support select lists
233+
//validateResultType( sqmStatement, sqmRoot, resultType, hql );
234+
235+
//noinspection unchecked
236+
return (SqmRoot<T>) sqmRoot;
237+
}
238+
239+
private void validateRoot(SqmRoot<?> sqmRoot, Class<T> resultType, String hql) {
226240
if ( sqmRoot.getJavaType() != null
227241
&& !Map.class.isAssignableFrom( sqmRoot.getJavaType() )
228242
&& !resultType.isAssignableFrom( sqmRoot.getJavaType() ) ) {
@@ -236,7 +250,65 @@ private SqmRoot<T> extractRoot(SqmSelectStatement<T> sqmStatement, Class<T> resu
236250
hql
237251
);
238252
}
239-
//noinspection unchecked
240-
return (SqmRoot<T>) sqmRoot;
253+
}
254+
255+
/**
256+
* For future, allowing explicit select list.
257+
*/
258+
private void validateResultType(
259+
SqmSelectStatement<T> sqmStatement,
260+
SqmRoot<?> sqmRoot,
261+
Class<T> resultType,
262+
String hql) {
263+
if ( resultType == null || Object.class.equals( resultType ) || resultType.isArray() ) {
264+
// Nothing to validate in these cases
265+
return;
266+
}
267+
268+
final Class<?> rootJavaType = sqmRoot.getJavaType();
269+
assert rootJavaType != null;
270+
271+
if ( Map.class.isAssignableFrom( rootJavaType ) ) {
272+
if ( Map.class.isAssignableFrom( resultType ) ) {
273+
// dynamic model and Map was requested, totally fine
274+
return;
275+
}
276+
}
277+
278+
final SqmSelectClause sqmSelectClause = sqmStatement.getQuerySpec().getSelectClause();
279+
final List<SqmSelection<?>> sqmSelections = sqmSelectClause.getSelections();
280+
if ( CollectionHelper.isEmpty( sqmSelections ) ) {
281+
// implicit select clause, verify that resultType matches the root type
282+
if ( resultType.isAssignableFrom( rootJavaType ) ) {
283+
// it does, we are fine
284+
return;
285+
}
286+
}
287+
else if ( sqmSelections.size() > 1 ) {
288+
// we have to assume we can.
289+
// the Query will ultimately complain if not, but this is the most we can do here
290+
return;
291+
}
292+
else {
293+
assert sqmSelections.size() == 1;
294+
final JavaType<?> nodeJavaType = sqmSelections.get( 0 ).getNodeJavaType();
295+
if ( nodeJavaType == null ) {
296+
// again, we have to assume we can
297+
return;
298+
}
299+
else if ( resultType.isAssignableFrom( nodeJavaType.getJavaTypeClass() ) ) {
300+
// it matches the selection type, we are fine
301+
return;
302+
}
303+
}
304+
305+
throw new QueryException(
306+
String.format(
307+
Locale.ROOT,
308+
"Specified result-type [%s] is not valid for this SelectionSpecification",
309+
resultType.getName()
310+
),
311+
hql
312+
);
241313
}
242314
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
6+
/**
7+
* Support for {@linkplain org.hibernate.query.specification.SelectionSpecification}
8+
* and {@linkplain org.hibernate.query.specification.MutationSpecification} which
9+
* is similar in concept to criteria queries, but focused on ease-of-use and less verbosity.
10+
*
11+
* @author Steve Ebersole
12+
*/
13+
package org.hibernate.query.specification;

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

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import java.util.Collection;
1010
import java.util.Date;
1111
import java.util.HashSet;
12-
import java.util.List;
1312
import java.util.Map;
1413
import java.util.Set;
1514

@@ -36,8 +35,6 @@
3635
import org.hibernate.query.IllegalQueryOperationException;
3736
import org.hibernate.query.KeyedPage;
3837
import org.hibernate.query.KeyedResultList;
39-
import org.hibernate.query.Order;
40-
import org.hibernate.query.Query;
4138
import org.hibernate.query.QueryParameter;
4239
import org.hibernate.query.ResultListTransformer;
4340
import org.hibernate.query.TupleTransformer;
@@ -290,16 +287,6 @@ public QueryImplementor<R> setLockMode(LockModeType lockModeType) {
290287
return this;
291288
}
292289

293-
@Override
294-
public Query<R> setOrder(List<? extends Order<? super R>> orders) {
295-
throw new UnsupportedOperationException( "Should be implemented by " + this.getClass().getName() );
296-
}
297-
298-
@Override
299-
public Query<R> setOrder(Order<? super R> order) {
300-
throw new UnsupportedOperationException( "Should be implemented by " + this.getClass().getName() );
301-
}
302-
303290
@Override
304291
public String getComment() {
305292
return super.getComment();

0 commit comments

Comments
 (0)