Skip to content
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 @@ -44,7 +44,6 @@
import org.hibernate.query.BindableType;
import org.hibernate.query.KeyedPage;
import org.hibernate.query.KeyedResultList;
import org.hibernate.query.Order;
import org.hibernate.query.OutputableType;
import org.hibernate.query.Query;
import org.hibernate.query.QueryParameter;
Expand Down Expand Up @@ -411,16 +410,6 @@ public QueryParameterBindings getParameterBindings() {
return parameterBindings;
}

@Override
public Query<R> setOrder(List<? extends Order<? super R>> orderList) {
throw new UnsupportedOperationException("Ordering not supported for stored procedure calls");
}

@Override
public Query<R> setOrder(Order<? super R> order) {
throw new UnsupportedOperationException("Ordering not supported for stored procedure calls");
}


// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Parameter registrations
Expand Down
8 changes: 4 additions & 4 deletions hibernate-core/src/main/java/org/hibernate/query/Order.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* <p>
* This is a convenience class which allows query result ordering rules to be
* passed around the system before being applied to a {@link Query} by calling
* {@link org.hibernate.query.programmatic.SelectionSpecification#sort(Order)}.
* {@link org.hibernate.query.specification.SelectionSpecification#sort(Order)}.
* <pre>
* SelectionSpecification.create(Book.class,
* "from Book b join b.authors a where a.name = :name")
Expand All @@ -31,7 +31,7 @@
* </pre>
* <p>
* {@code Order}s may be stacked using {@link List#of} and
* {@link org.hibernate.query.programmatic.SelectionSpecification#resort(List)}.
* {@link org.hibernate.query.specification.SelectionSpecification#resort(List)}.
* <pre>
* SelectionSpecification.create(Book.class,
* "from Book b join b.authors a where a.name = :name")
Expand All @@ -52,8 +52,8 @@
* used by Hibernate Data Repositories to implement Jakarta Data
* query methods.
*
* @see org.hibernate.query.programmatic.SelectionSpecification#sort(Order)
* @see org.hibernate.query.programmatic.SelectionSpecification#resort(List)
* @see org.hibernate.query.specification.SelectionSpecification#sort(Order)
* @see org.hibernate.query.specification.SelectionSpecification#resort(List)
* @see org.hibernate.query.restriction.Restriction
*
* @author Gavin King
Expand Down
5 changes: 0 additions & 5 deletions hibernate-core/src/main/java/org/hibernate/query/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -928,11 +928,6 @@ default Query<R> setPage(Page page) {
@Override
Query<R> setLockMode(LockModeType lockMode);

@Override @Incubating
Query<R> setOrder(List<? extends Order<? super R>> orderList);

@Override @Incubating
Query<R> setOrder(Order<? super R> order);

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// deprecated methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -596,32 +596,6 @@ default Stream<R> stream() {
*/
SelectionQuery<R> setLockMode(String alias, LockMode lockMode);

/**
* If the result type of this query is an entity class, add one or more
* {@linkplain Order rules} for ordering the query results.
*
* @param orderList one or more instances of {@link Order}
*
* @see Order
*
* @since 6.3
*/
@Incubating
SelectionQuery<R> setOrder(List<? extends Order<? super R>> orderList);

/**
* If the result type of this query is an entity class, add a
* {@linkplain Order rule} for ordering the query results.
*
* @param order an instance of {@link Order}
*
* @see Order
*
* @since 6.3
*/
@Incubating
SelectionQuery<R> setOrder(Order<? super R> order);

/**
* Specifies whether follow-on locking should be applied
*/
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

/**
* A rule for restricting query results. This allows restrictions to be added to
* a {@link org.hibernate.query.programmatic.SelectionSpecification} by calling
* {@link org.hibernate.query.programmatic.SelectionSpecification#restrict(Restriction)}.
* a {@link org.hibernate.query.specification.SelectionSpecification} by calling
* {@link org.hibernate.query.specification.SelectionSpecification#restrict(Restriction)}.
* <pre>
* SelectionSpecification.create(Book.class)
* .restrict(Restriction.like(Book_.title, "%Hibernate%", false))
Expand All @@ -47,9 +47,9 @@
* is used by Hibernate Data Repositories to implement Jakarta Data
* query methods.
*
* @see org.hibernate.query.programmatic.SelectionSpecification
* @see org.hibernate.query.programmatic.MutationSpecification
* @see org.hibernate.query.programmatic.QuerySpecification#restrict(Restriction)
* @see org.hibernate.query.specification.SelectionSpecification
* @see org.hibernate.query.specification.MutationSpecification
* @see org.hibernate.query.specification.QuerySpecification#restrict(Restriction)
*
* @see Path
* @see Order
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* SPDX-License-Identifier: Apache-2.0
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.query.programmatic;
package org.hibernate.query.specification;

import jakarta.persistence.criteria.CommonAbstractCriteria;
import jakarta.persistence.criteria.CriteriaUpdate;
Expand All @@ -14,7 +14,7 @@
import org.hibernate.StatelessSession;
import org.hibernate.query.IllegalMutationQueryException;
import org.hibernate.query.MutationQuery;
import org.hibernate.query.programmatic.internal.MutationSpecificationImpl;
import org.hibernate.query.specification.internal.MutationSpecificationImpl;
import org.hibernate.query.restriction.Restriction;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* SPDX-License-Identifier: Apache-2.0
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.query.programmatic;
package org.hibernate.query.specification;

import jakarta.persistence.EntityManager;
import jakarta.persistence.criteria.CriteriaBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* SPDX-License-Identifier: Apache-2.0
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.query.programmatic;
package org.hibernate.query.specification;

import jakarta.persistence.EntityManager;
import jakarta.persistence.criteria.CriteriaQuery;
Expand All @@ -15,7 +15,7 @@
import org.hibernate.query.IllegalSelectQueryException;
import org.hibernate.query.Order;
import org.hibernate.query.SelectionQuery;
import org.hibernate.query.programmatic.internal.SelectionSpecificationImpl;
import org.hibernate.query.specification.internal.SelectionSpecificationImpl;
import org.hibernate.query.restriction.Path;
import org.hibernate.query.restriction.Restriction;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* SPDX-License-Identifier: Apache-2.0
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.query.programmatic.internal;
package org.hibernate.query.specification.internal;

import jakarta.persistence.EntityManager;
import jakarta.persistence.criteria.CriteriaBuilder;
Expand All @@ -15,7 +15,7 @@
import org.hibernate.SharedSessionContract;
import org.hibernate.StatelessSession;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.query.programmatic.MutationSpecification;
import org.hibernate.query.specification.MutationSpecification;
import org.hibernate.query.IllegalMutationQueryException;
import org.hibernate.query.MutationQuery;
import org.hibernate.query.restriction.Restriction;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* SPDX-License-Identifier: Apache-2.0
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.query.programmatic.internal;
package org.hibernate.query.specification.internal;

import jakarta.persistence.EntityManager;
import jakarta.persistence.criteria.CriteriaQuery;
Expand All @@ -13,10 +13,11 @@
import org.hibernate.SharedSessionContract;
import org.hibernate.StatelessSession;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.query.IllegalSelectQueryException;
import org.hibernate.query.Order;
import org.hibernate.query.SelectionQuery;
import org.hibernate.query.programmatic.SelectionSpecification;
import org.hibernate.query.specification.SelectionSpecification;
import org.hibernate.query.restriction.Path;
import org.hibernate.query.restriction.Restriction;
import org.hibernate.query.spi.HqlInterpretation;
Expand All @@ -28,8 +29,11 @@
import org.hibernate.query.sqm.tree.from.SqmRoot;
import org.hibernate.query.sqm.tree.predicate.SqmPredicate;
import org.hibernate.query.sqm.tree.select.SqmOrderByClause;
import org.hibernate.query.sqm.tree.select.SqmSelectClause;
import org.hibernate.query.sqm.tree.select.SqmSelectStatement;
import org.hibernate.query.sqm.tree.select.SqmSelection;
import org.hibernate.query.sqm.tree.select.SqmSortSpecification;
import org.hibernate.type.descriptor.java.JavaType;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -159,8 +163,8 @@ else if ( criteriaQuery != null ) {
var query = builder.createQuery( resultType );
var root = query.from( resultType );
query.select( root );
sqmRoot = (SqmRoot<T>) root;
sqmStatement = (SqmSelectStatement<T>) query;
sqmRoot = root;
sqmStatement = query;
}
specifications.forEach( consumer -> consumer.accept( sqmStatement, sqmRoot ) );
return sqmStatement;
Expand Down Expand Up @@ -222,7 +226,17 @@ private SqmRoot<T> extractRoot(SqmSelectStatement<T> sqmStatement, Class<T> resu
if ( sqmRoots.size() > 1 ) {
throw new QueryException( "Query defined multiple roots", hql );
}

final SqmRoot<?> sqmRoot = sqmRoots.iterator().next();
validateRoot( sqmRoot, resultType, hql );
// for later, to support select lists
//validateResultType( sqmStatement, sqmRoot, resultType, hql );

//noinspection unchecked
return (SqmRoot<T>) sqmRoot;
}

private void validateRoot(SqmRoot<?> sqmRoot, Class<T> resultType, String hql) {
if ( sqmRoot.getJavaType() != null
&& !Map.class.isAssignableFrom( sqmRoot.getJavaType() )
&& !resultType.isAssignableFrom( sqmRoot.getJavaType() ) ) {
Expand All @@ -236,7 +250,65 @@ private SqmRoot<T> extractRoot(SqmSelectStatement<T> sqmStatement, Class<T> resu
hql
);
}
//noinspection unchecked
return (SqmRoot<T>) sqmRoot;
}

/**
* For future, allowing explicit select list.
*/
private void validateResultType(
SqmSelectStatement<T> sqmStatement,
SqmRoot<?> sqmRoot,
Class<T> resultType,
String hql) {
if ( resultType == null || Object.class.equals( resultType ) || resultType.isArray() ) {
// Nothing to validate in these cases
return;
}

final Class<?> rootJavaType = sqmRoot.getJavaType();
assert rootJavaType != null;

if ( Map.class.isAssignableFrom( rootJavaType ) ) {
if ( Map.class.isAssignableFrom( resultType ) ) {
// dynamic model and Map was requested, totally fine
return;
}
}

final SqmSelectClause sqmSelectClause = sqmStatement.getQuerySpec().getSelectClause();
final List<SqmSelection<?>> sqmSelections = sqmSelectClause.getSelections();
if ( CollectionHelper.isEmpty( sqmSelections ) ) {
// implicit select clause, verify that resultType matches the root type
if ( resultType.isAssignableFrom( rootJavaType ) ) {
// it does, we are fine
return;
}
}
else if ( sqmSelections.size() > 1 ) {
// we have to assume we can.
// the Query will ultimately complain if not, but this is the most we can do here
return;
}
else {
assert sqmSelections.size() == 1;
final JavaType<?> nodeJavaType = sqmSelections.get( 0 ).getNodeJavaType();
if ( nodeJavaType == null ) {
// again, we have to assume we can
return;
}
else if ( resultType.isAssignableFrom( nodeJavaType.getJavaTypeClass() ) ) {
// it matches the selection type, we are fine
return;
}
}

throw new QueryException(
String.format(
Locale.ROOT,
"Specified result-type [%s] is not valid for this SelectionSpecification",
resultType.getName()
),
hql
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright Red Hat Inc. and Hibernate Authors
*/

/**
* Support for {@linkplain org.hibernate.query.specification.SelectionSpecification}
* and {@linkplain org.hibernate.query.specification.MutationSpecification} which
* is similar in concept to criteria queries, but focused on ease-of-use and less verbosity.
*
* @author Steve Ebersole
*/
package org.hibernate.query.specification;
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

Expand All @@ -36,8 +35,6 @@
import org.hibernate.query.IllegalQueryOperationException;
import org.hibernate.query.KeyedPage;
import org.hibernate.query.KeyedResultList;
import org.hibernate.query.Order;
import org.hibernate.query.Query;
import org.hibernate.query.QueryParameter;
import org.hibernate.query.ResultListTransformer;
import org.hibernate.query.TupleTransformer;
Expand Down Expand Up @@ -290,16 +287,6 @@ public QueryImplementor<R> setLockMode(LockModeType lockModeType) {
return this;
}

@Override
public Query<R> setOrder(List<? extends Order<? super R>> orders) {
throw new UnsupportedOperationException( "Should be implemented by " + this.getClass().getName() );
}

@Override
public Query<R> setOrder(Order<? super R> order) {
throw new UnsupportedOperationException( "Should be implemented by " + this.getClass().getName() );
}

@Override
public String getComment() {
return super.getComment();
Expand Down
Loading