HHH-16815 more flexible sorting via Query API

- added Query.sort(SortOrder,SingularAttribute)
- added Query.sort(Sort...)
- added Sort class for convenience
- moved SortOrder to org.hibernate.query
  (its package was @Incubating)
- move NullPrecedence enum
This commit is contained in:
Gavin King 2023-06-28 13:39:53 +02:00
parent 8b6404eae6
commit f7e12d49ed
48 changed files with 274 additions and 78 deletions

View File

@ -240,4 +240,8 @@ public interface SharedSessionContract extends QueryProducer, Closeable, Seriali
throw new UnsupportedOperationException();
}
/**
* The factory which created this session.
*/
SessionFactory getFactory();
}

View File

@ -18,7 +18,7 @@ import org.hibernate.context.spi.CurrentTenantIdentifierResolver;
import org.hibernate.jpa.spi.JpaCompliance;
import org.hibernate.loader.BatchFetchStyle;
import org.hibernate.proxy.EntityNotFoundDelegate;
import org.hibernate.query.sqm.NullPrecedence;
import org.hibernate.query.NullPrecedence;
import org.hibernate.query.sqm.function.SqmFunctionDescriptor;
import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode;
import org.hibernate.resource.jdbc.spi.StatementInspector;

View File

@ -27,7 +27,7 @@ import org.hibernate.context.spi.CurrentTenantIdentifierResolver;
import org.hibernate.internal.SessionFactoryImpl;
import org.hibernate.loader.BatchFetchStyle;
import org.hibernate.proxy.EntityNotFoundDelegate;
import org.hibernate.query.sqm.NullPrecedence;
import org.hibernate.query.NullPrecedence;
import org.hibernate.query.sqm.function.SqmFunctionDescriptor;
import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode;
import org.hibernate.resource.jdbc.spi.StatementInspector;

View File

@ -56,7 +56,7 @@ import org.hibernate.proxy.EntityNotFoundDelegate;
import org.hibernate.query.ImmutableEntityUpdateQueryHandlingMode;
import org.hibernate.query.criteria.ValueHandlingMode;
import org.hibernate.query.hql.HqlTranslator;
import org.hibernate.query.sqm.NullPrecedence;
import org.hibernate.query.NullPrecedence;
import org.hibernate.query.sqm.function.SqmFunctionDescriptor;
import org.hibernate.query.sqm.function.SqmFunctionRegistry;
import org.hibernate.query.sqm.mutation.spi.SqmMultiTableInsertStrategy;

View File

@ -19,7 +19,7 @@ import org.hibernate.cache.spi.TimestampsCacheFactory;
import org.hibernate.context.spi.CurrentTenantIdentifierResolver;
import org.hibernate.loader.BatchFetchStyle;
import org.hibernate.proxy.EntityNotFoundDelegate;
import org.hibernate.query.sqm.NullPrecedence;
import org.hibernate.query.NullPrecedence;
import org.hibernate.query.sqm.function.SqmFunctionDescriptor;
import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode;
import org.hibernate.resource.jdbc.spi.StatementInspector;

View File

@ -27,7 +27,7 @@ import org.hibernate.proxy.EntityNotFoundDelegate;
import org.hibernate.query.ImmutableEntityUpdateQueryHandlingMode;
import org.hibernate.query.criteria.ValueHandlingMode;
import org.hibernate.query.hql.HqlTranslator;
import org.hibernate.query.sqm.NullPrecedence;
import org.hibernate.query.NullPrecedence;
import org.hibernate.query.sqm.function.SqmFunctionDescriptor;
import org.hibernate.query.sqm.function.SqmFunctionRegistry;
import org.hibernate.query.sqm.mutation.spi.SqmMultiTableInsertStrategy;

View File

@ -28,7 +28,7 @@ import org.hibernate.proxy.EntityNotFoundDelegate;
import org.hibernate.query.ImmutableEntityUpdateQueryHandlingMode;
import org.hibernate.query.criteria.ValueHandlingMode;
import org.hibernate.query.spi.QueryEngineOptions;
import org.hibernate.query.sqm.NullPrecedence;
import org.hibernate.query.NullPrecedence;
import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode;
import org.hibernate.resource.jdbc.spi.StatementInspector;
import org.hibernate.stat.Statistics;

View File

@ -23,7 +23,7 @@ import org.hibernate.id.enhanced.ImplicitDatabaseObjectNamingStrategy;
import org.hibernate.jpa.LegacySpecHints;
import org.hibernate.jpa.SpecHints;
import org.hibernate.query.spi.QueryPlan;
import org.hibernate.query.sqm.NullPrecedence;
import org.hibernate.query.NullPrecedence;
import org.hibernate.query.sqm.mutation.internal.temptable.GlobalTemporaryTableStrategy;
import org.hibernate.query.sqm.mutation.internal.temptable.LocalTemporaryTableStrategy;
import org.hibernate.query.sqm.mutation.internal.temptable.PersistentTableStrategy;

View File

@ -57,7 +57,7 @@ import jakarta.persistence.metamodel.Metamodel;
* instance itself is lazily provided via a {@code Supplier}.
* When the decorated instance is readily available, one
* should prefer using {@code SessionDelegatorBaseImpl}.
*
* <p>
* Another difference with SessionDelegatorBaseImpl is that
* this type only implements Session.
*
@ -67,6 +67,11 @@ public class SessionLazyDelegator implements Session {
private final Supplier<Session> lazySession;
@Override
public SessionFactory getFactory() {
return lazySession.get().getFactory();
}
public SessionLazyDelegator(Supplier<Session> lazySessionLookup){
this.lazySession = lazySessionLookup;
}
@ -505,7 +510,6 @@ public class SessionLazyDelegator implements Session {
return this.lazySession.get().getLobHelper();
}
@SuppressWarnings("rawtypes")
@Override
public SharedSessionBuilder sessionWithOptions() {
return this.lazySession.get().sessionWithOptions();

View File

@ -16,8 +16,8 @@ import org.hibernate.metamodel.mapping.ModelPart;
import org.hibernate.metamodel.mapping.SelectableMapping;
import org.hibernate.metamodel.mapping.ordering.ast.DomainPath;
import org.hibernate.metamodel.mapping.ordering.ast.OrderingExpression;
import org.hibernate.query.sqm.NullPrecedence;
import org.hibernate.query.sqm.SortOrder;
import org.hibernate.query.NullPrecedence;
import org.hibernate.query.SortOrder;
import org.hibernate.sql.ast.spi.SqlAstCreationState;
import org.hibernate.sql.ast.spi.SqlSelection;
import org.hibernate.sql.ast.tree.SqlAstNode;

View File

@ -12,8 +12,8 @@ import org.hibernate.metamodel.mapping.ModelPartContainer;
import org.hibernate.metamodel.mapping.PluralAttributeMapping;
import org.hibernate.metamodel.mapping.ordering.TranslationContext;
import org.hibernate.persister.entity.AbstractEntityPersister;
import org.hibernate.query.sqm.NullPrecedence;
import org.hibernate.query.sqm.SortOrder;
import org.hibernate.query.NullPrecedence;
import org.hibernate.query.SortOrder;
import org.hibernate.sql.ast.spi.SqlAstCreationState;
import org.hibernate.sql.ast.spi.SqlExpressionResolver;
import org.hibernate.sql.ast.tree.expression.Expression;

View File

@ -12,8 +12,8 @@ import java.util.List;
import org.hibernate.metamodel.mapping.CollectionPart;
import org.hibernate.metamodel.mapping.internal.AbstractDomainPath;
import org.hibernate.query.sqm.NullPrecedence;
import org.hibernate.query.sqm.SortOrder;
import org.hibernate.query.NullPrecedence;
import org.hibernate.query.SortOrder;
import org.hibernate.query.sqm.function.FunctionRenderingSupport;
import org.hibernate.query.sqm.function.SelfRenderingFunctionSqlAstExpression;
import org.hibernate.sql.ast.SqlAstTranslator;

View File

@ -6,8 +6,8 @@
*/
package org.hibernate.metamodel.mapping.ordering.ast;
import org.hibernate.query.sqm.NullPrecedence;
import org.hibernate.query.sqm.SortOrder;
import org.hibernate.query.NullPrecedence;
import org.hibernate.query.SortOrder;
import org.hibernate.query.spi.QueryEngine;
import org.hibernate.query.sqm.sql.FakeSqmToSqlAstConverter;
import org.hibernate.query.sqm.sql.SqmToSqlAstConverter;

View File

@ -6,8 +6,8 @@
*/
package org.hibernate.metamodel.mapping.ordering.ast;
import org.hibernate.query.sqm.NullPrecedence;
import org.hibernate.query.sqm.SortOrder;
import org.hibernate.query.NullPrecedence;
import org.hibernate.query.SortOrder;
/**
* An individual sort specification in an order-by fragment

View File

@ -12,8 +12,8 @@ import java.util.List;
import java.util.Locale;
import org.hibernate.internal.util.QuotingHelper;
import org.hibernate.query.sqm.NullPrecedence;
import org.hibernate.query.sqm.SortOrder;
import org.hibernate.query.NullPrecedence;
import org.hibernate.query.SortOrder;
import org.hibernate.grammars.ordering.OrderingParser;
import org.hibernate.grammars.ordering.OrderingParserBaseVisitor;
import org.hibernate.metamodel.mapping.PluralAttributeMapping;

View File

@ -6,8 +6,8 @@
*/
package org.hibernate.metamodel.mapping.ordering.ast;
import org.hibernate.query.sqm.NullPrecedence;
import org.hibernate.query.sqm.SortOrder;
import org.hibernate.query.NullPrecedence;
import org.hibernate.query.SortOrder;
import org.hibernate.sql.ast.spi.SqlAstCreationState;
import org.hibernate.sql.ast.tree.expression.Expression;
import org.hibernate.sql.ast.tree.expression.SelfRenderingSqlFragmentExpression;

View File

@ -49,6 +49,7 @@ import org.hibernate.query.BindableType;
import org.hibernate.query.OutputableType;
import org.hibernate.query.Query;
import org.hibernate.query.QueryParameter;
import org.hibernate.query.SortOrder;
import org.hibernate.query.internal.QueryOptionsImpl;
import org.hibernate.query.procedure.ProcedureParameter;
import org.hibernate.query.results.ResultSetMapping;
@ -1074,6 +1075,11 @@ public class ProcedureCallImpl<R>
throw new UnsupportedOperationException( "Not supported for procedure calls" );
}
@Override
public Query<R> sort(SortOrder sortOrder, SingularAttribute<? super R, ?> attribute) {
throw new UnsupportedOperationException( "Not supported for procedure calls" );
}
@Override
public Query<R> ascending(int element) {
throw new UnsupportedOperationException( "Not supported for procedure calls" );
@ -1085,7 +1091,7 @@ public class ProcedureCallImpl<R>
}
@Override
public Query<R> unordered() {
public Query<R> clearOrder() {
return this;
}

View File

@ -4,7 +4,9 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.query.sqm;
package org.hibernate.query;
import org.hibernate.query.sqm.NullOrdering;
/**
* Defines precedence of null values within {@code ORDER BY} clause.

View File

@ -905,6 +905,24 @@ public interface Query<R> extends SelectionQuery<R>, MutationQuery, TypedQuery<R
@Override
Query<R> descending(SingularAttribute<? super R, ?> attribute);
@Override
Query<R> sort(SortOrder sortOrder, SingularAttribute<? super R, ?> attribute);
@Override
default Query<R> sort(Sort<? super R>... sorts) {
for (Sort<? super R> sort: sorts) {
SingularAttribute<? super R,?> attribute = sort.getAttribute();
if ( attribute == null ) {
attribute =
getSession().getFactory().getMetamodel()
.entity( sort.getEntityClass() )
.getSingularAttribute( sort.getAttributeName() );
}
sort( sort.getOrder(), attribute );
}
return this;
}
@Override
Query<R> ascending(int element);
@ -912,7 +930,7 @@ public interface Query<R> extends SelectionQuery<R>, MutationQuery, TypedQuery<R
Query<R> descending(int element);
@Override
Query<R> unordered();
Query<R> clearOrder();
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// deprecated methods

View File

@ -493,6 +493,28 @@ public interface SelectionQuery<R> extends CommonQueryContract {
@Incubating
SelectionQuery<R> descending(SingularAttribute<? super R, ?> attribute);
/**
* If the result type of this query is an entity class, add an attribute
* of the entity to be used to order the query results in the given order.
*
* @param attribute an attribute of the entity class returned by this query
*
* @since 6.3
*/
@Incubating
SelectionQuery<R> sort(SortOrder sortOrder, SingularAttribute<? super R, ?> attribute);
/**
* If the result type of this query is an entity class, add one or more
* {@linkplain Sort rules} for ordering the query results.
*
* @param sorts one or more instances of {@link Sort}
*
* @since 6.3
*/
@Incubating
SelectionQuery<R> sort(Sort<? super R>... sorts);
/**
* Add an element of the select list to be used to order the query results
* in ascending order.
@ -516,7 +538,9 @@ public interface SelectionQuery<R> extends CommonQueryContract {
SelectionQuery<R> descending(int element);
/**
* Clear the ordering conditions for this query.
* Clear the ordering conditions for this query. Discards any ordering
* conditions added via {@link #ascending}, {@link #descending}, or
* {@link #sort}.
*
* @see #ascending(SingularAttribute)
* @see #descending(SingularAttribute)
@ -524,7 +548,7 @@ public interface SelectionQuery<R> extends CommonQueryContract {
* @since 6.3
*/
@Incubating
SelectionQuery<R> unordered();
SelectionQuery<R> clearOrder();
/**
* Specify a {@link LockMode} to apply to a specific alias defined in the query

View File

@ -0,0 +1,94 @@
package org.hibernate.query;
import jakarta.persistence.metamodel.SingularAttribute;
/**
* A rule for sorting an entity type in a query result set.
* <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 Query#sort(Sort[])}.
*
* @param <X> The type of the entity to be sorted
*
* @see Query#sort(Sort[])
*
* @author Gavin King
*
* @since 6.3
*/
public class Sort<X> {
private final SortOrder order;
private final SingularAttribute<X,?> attribute;
private final Class<X> entityClass;
private final String attributeName;
public Sort(SortOrder order, SingularAttribute<X, ?> attribute) {
this.order = order;
this.attribute = attribute;
this.attributeName = attribute.getName();
this.entityClass = attribute.getDeclaringType().getJavaType();
}
public Sort(SortOrder order, Class<X> entityClass, String attributeName) {
this.order = order;
this.entityClass = entityClass;
this.attributeName = attributeName;
this.attribute = null;
}
public static <T> Sort<T> asc(SingularAttribute<T,?> attribute) {
return new Sort<>(SortOrder.ASCENDING, attribute);
}
public static <T> Sort<T> desc(SingularAttribute<T,?> attribute) {
return new Sort<>(SortOrder.ASCENDING, attribute);
}
public static <T> Sort<T> asc(Class<T> entityClass, String attributeName) {
return new Sort<>( SortOrder.ASCENDING, entityClass, attributeName );
}
public static <T> Sort<T> desc(Class<T> entityClass, String attributeName) {
return new Sort<>( SortOrder.ASCENDING, entityClass, attributeName );
}
public SortOrder getOrder() {
return order;
}
public SingularAttribute<X, ?> getAttribute() {
return attribute;
}
public Class<X> getEntityClass() {
return entityClass;
}
public String getAttributeName() {
return attributeName;
}
@Override
public String toString() {
return attributeName + " " + order;
}
@Override
public boolean equals(Object o) {
if ( o instanceof Sort ) {
Sort<?> that = (Sort<?>) o;
return that.order == order
&& that.attributeName.equals(attributeName)
&& that.entityClass.equals(entityClass);
}
else {
return false;
}
}
@Override
public int hashCode() {
return attributeName.hashCode() + entityClass.hashCode();
}
}

View File

@ -4,12 +4,16 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
*/
package org.hibernate.query.sqm;
package org.hibernate.query;
import java.util.Locale;
/**
* Enumerates the directions in which query results may be sorted.
*
* @author Steve Ebersole
*
* @see Sort
*/
public enum SortOrder {
ASCENDING,

View File

@ -35,8 +35,8 @@ import jakarta.persistence.criteria.Subquery;
import org.hibernate.Incubating;
import org.hibernate.query.sqm.FrameKind;
import org.hibernate.query.sqm.NullPrecedence;
import org.hibernate.query.sqm.SortOrder;
import org.hibernate.query.NullPrecedence;
import org.hibernate.query.SortOrder;
import org.hibernate.query.sqm.TemporalUnit;
import org.hibernate.query.sqm.tree.expression.SqmExpression;

View File

@ -8,8 +8,8 @@ package org.hibernate.query.criteria;
import jakarta.persistence.criteria.Order;
import org.hibernate.query.sqm.NullPrecedence;
import org.hibernate.query.sqm.SortOrder;
import org.hibernate.query.NullPrecedence;
import org.hibernate.query.SortOrder;
/**
* @author Steve Ebersole

View File

@ -7,10 +7,8 @@
package org.hibernate.query.criteria;
import org.hibernate.Incubating;
import org.hibernate.query.sqm.NullPrecedence;
import org.hibernate.query.sqm.SortOrder;
import jakarta.persistence.criteria.Order;
import org.hibernate.query.NullPrecedence;
import org.hibernate.query.SortOrder;
/**
* Represents the search order for a recursive CTE (common table expression).

View File

@ -51,8 +51,8 @@ import org.hibernate.query.criteria.JpaSimpleCase;
import org.hibernate.query.criteria.JpaSubQuery;
import org.hibernate.query.criteria.JpaWindow;
import org.hibernate.query.criteria.JpaWindowFrame;
import org.hibernate.query.sqm.NullPrecedence;
import org.hibernate.query.sqm.SortOrder;
import org.hibernate.query.NullPrecedence;
import org.hibernate.query.SortOrder;
import org.hibernate.query.sqm.TemporalUnit;
import org.hibernate.query.sqm.tree.expression.SqmExpression;

View File

@ -79,10 +79,10 @@ import org.hibernate.query.sqm.FrameKind;
import org.hibernate.query.sqm.FrameMode;
import org.hibernate.query.sqm.LiteralNumberFormatException;
import org.hibernate.query.sqm.NodeBuilder;
import org.hibernate.query.sqm.NullPrecedence;
import org.hibernate.query.NullPrecedence;
import org.hibernate.query.sqm.ParsingException;
import org.hibernate.query.sqm.SetOperator;
import org.hibernate.query.sqm.SortOrder;
import org.hibernate.query.SortOrder;
import org.hibernate.query.sqm.SqmExpressible;
import org.hibernate.query.sqm.SqmPathSource;
import org.hibernate.query.sqm.SqmQuerySource;

View File

@ -22,13 +22,14 @@ import org.hibernate.graph.RootGraph;
import org.hibernate.query.BindableType;
import org.hibernate.query.QueryParameter;
import org.hibernate.query.ResultListTransformer;
import org.hibernate.query.SelectionQuery;
import org.hibernate.query.TupleTransformer;
import org.hibernate.query.named.NameableQuery;
import org.hibernate.query.named.NamedQueryMemento;
import org.hibernate.query.spi.ParameterMetadataImplementor;
import org.hibernate.query.spi.QueryImplementor;
import org.hibernate.query.spi.SqmQuery;
import org.hibernate.query.sqm.SortOrder;
import org.hibernate.query.SortOrder;
import org.hibernate.query.sqm.tree.SqmStatement;
import org.hibernate.transform.ResultTransformer;
@ -136,6 +137,12 @@ public interface SqmQueryImplementor<R> extends QueryImplementor<R>, SqmQuery, N
return this;
}
@Override
default SqmQueryImplementor<R> sort(SortOrder sortOrder, SingularAttribute<? super R, ?> attribute) {
addOrdering( attribute, sortOrder );
return this;
}
@Override
default SqmQueryImplementor<R> ascending(int element) {
addOrdering( element, SortOrder.ASCENDING );
@ -153,7 +160,7 @@ public interface SqmQueryImplementor<R> extends QueryImplementor<R>, SqmQuery, N
SqmQueryImplementor<R> addOrdering(int element, SortOrder order);
@Override
SqmQueryImplementor<R> unordered();
SqmQueryImplementor<R> clearOrder();
@Override
SqmQueryImplementor<R> setParameter(String name, Object value);

View File

@ -39,6 +39,7 @@ import org.hibernate.query.IllegalQueryOperationException;
import org.hibernate.query.Query;
import org.hibernate.query.QueryParameter;
import org.hibernate.query.ResultListTransformer;
import org.hibernate.query.SortOrder;
import org.hibernate.query.TupleTransformer;
import org.hibernate.query.named.NamedQueryMemento;
@ -287,6 +288,11 @@ public abstract class AbstractQuery<R>
throw new UnsupportedOperationException( "Should be implemented by " + this.getClass().getName() );
}
@Override
public Query<R> sort(SortOrder sortOrder, SingularAttribute<? super R, ?> attribute) {
throw new UnsupportedOperationException( "Should be implemented by " + this.getClass().getName() );
}
@Override
public Query<R> ascending(int element) {
throw new UnsupportedOperationException( "Should be implemented by " + this.getClass().getName() );
@ -298,7 +304,7 @@ public abstract class AbstractQuery<R>
}
@Override
public Query<R> unordered() {
public Query<R> clearOrder() {
throw new UnsupportedOperationException( "Should be implemented by " + this.getClass().getName() );
}

View File

@ -48,6 +48,7 @@ import org.hibernate.query.PathException;
import org.hibernate.query.Query;
import org.hibernate.query.QueryParameter;
import org.hibernate.query.ResultListTransformer;
import org.hibernate.query.SortOrder;
import org.hibernate.query.TupleTransformer;
import org.hibernate.query.internal.ParameterMetadataImpl;
import org.hibernate.query.internal.QueryOptionsImpl;
@ -1510,6 +1511,11 @@ public class NativeQueryImpl<R>
throw new UnsupportedOperationException("Not yet supported for native queries");
}
@Override
public Query<R> sort(SortOrder sortOrder, SingularAttribute<? super R, ?> attribute) {
throw new UnsupportedOperationException("Not yet supported for native queries");
}
@Override
public Query<R> ascending(int element) {
throw new UnsupportedOperationException("Not yet supported for native queries");
@ -1521,7 +1527,7 @@ public class NativeQueryImpl<R>
}
@Override
public Query<R> unordered() {
public Query<R> clearOrder() {
return this;
}

View File

@ -19,6 +19,8 @@ import java.util.Set;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.metamodel.model.domain.JpaMetamodel;
import org.hibernate.query.NullPrecedence;
import org.hibernate.query.SortOrder;
import org.hibernate.query.criteria.HibernateCriteriaBuilder;
import org.hibernate.query.criteria.JpaCoalesce;
import org.hibernate.query.criteria.JpaCompoundSelection;

View File

@ -78,7 +78,7 @@ import org.hibernate.query.spi.QueryParameterBindings;
import org.hibernate.query.spi.ScrollableResultsImplementor;
import org.hibernate.query.spi.SelectQueryPlan;
import org.hibernate.query.sqm.NodeBuilder;
import org.hibernate.query.sqm.SortOrder;
import org.hibernate.query.SortOrder;
import org.hibernate.query.sqm.SqmPathSource;
import org.hibernate.query.sqm.internal.SqmInterpretationsKey.InterpretationsKeySource;
import org.hibernate.query.sqm.mutation.spi.SqmMultiTableMutationStrategy;
@ -1004,7 +1004,7 @@ public class QuerySqmImpl<R>
}
@Override
public SqmQueryImplementor<R> unordered() {
public SqmQueryImplementor<R> clearOrder() {
if ( sqm instanceof SqmSelectStatement ) {
sqm = sqm.copy( SqmCopyContext.noParamCopyContext() );
SqmSelectStatement<R> select = (SqmSelectStatement<R>) sqm;

View File

@ -69,9 +69,9 @@ import org.hibernate.query.sqm.BinaryArithmeticOperator;
import org.hibernate.query.sqm.ComparisonOperator;
import org.hibernate.query.sqm.FrameKind;
import org.hibernate.query.sqm.NodeBuilder;
import org.hibernate.query.sqm.NullPrecedence;
import org.hibernate.query.NullPrecedence;
import org.hibernate.query.sqm.SetOperator;
import org.hibernate.query.sqm.SortOrder;
import org.hibernate.query.SortOrder;
import org.hibernate.query.sqm.SqmExpressible;
import org.hibernate.query.sqm.SqmQuerySource;
import org.hibernate.query.sqm.TemporalUnit;

View File

@ -35,8 +35,11 @@ import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.graph.spi.AppliedGraph;
import org.hibernate.internal.util.collections.IdentitySet;
import org.hibernate.query.BindableType;
import org.hibernate.query.Query;
import org.hibernate.query.QueryLogging;
import org.hibernate.query.QueryParameter;
import org.hibernate.query.SelectionQuery;
import org.hibernate.query.Sort;
import org.hibernate.query.criteria.internal.NamedCriteriaQueryMementoImpl;
import org.hibernate.query.hql.internal.NamedHqlQueryMementoImpl;
import org.hibernate.query.hql.internal.QuerySplitter;
@ -55,7 +58,7 @@ import org.hibernate.query.spi.QueryParameterBindings;
import org.hibernate.query.spi.ScrollableResultsImplementor;
import org.hibernate.query.spi.SelectQueryPlan;
import org.hibernate.query.sqm.NodeBuilder;
import org.hibernate.query.sqm.SortOrder;
import org.hibernate.query.SortOrder;
import org.hibernate.query.sqm.SqmSelectionQuery;
import org.hibernate.query.sqm.internal.SqmInterpretationsKey.InterpretationsKeySource;
import org.hibernate.query.sqm.tree.SqmCopyContext;
@ -569,6 +572,27 @@ public class SqmSelectionQueryImpl<R> extends AbstractSelectionQuery<R>
return this;
}
@Override
public SelectionQuery<R> sort(SortOrder sortOrder, SingularAttribute<? super R, ?> attribute) {
addOrdering( attribute, sortOrder );
return null;
}
@Override @SafeVarargs
public final SqmSelectionQuery<R> sort(Sort<? super R>... sorts) {
for (Sort<? super R> sort: sorts) {
SingularAttribute<? super R,?> attribute = sort.getAttribute();
if ( attribute == null ) {
attribute =
getSession().getFactory().getMetamodel()
.entity( sort.getEntityClass() )
.getSingularAttribute( sort.getAttributeName() );
}
sort( sort.getOrder(), attribute );
}
return this;
}
@Override
public SqmSelectionQuery<R> ascending(int element) {
addOrdering( element, SortOrder.ASCENDING );
@ -618,7 +642,7 @@ public class SqmSelectionQueryImpl<R> extends AbstractSelectionQuery<R>
}
@Override
public SqmSelectionQuery<R> unordered() {
public SqmSelectionQuery<R> clearOrder() {
sqm = sqm.copy( SqmCopyContext.noParamCopyContext() );
sqm.getQueryPart().setOrderByClause( null );
return this;

View File

@ -11,7 +11,7 @@ import java.util.Collections;
import java.util.List;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.query.sqm.SortOrder;
import org.hibernate.query.SortOrder;
import org.hibernate.query.sqm.function.SelfRenderingWindowFunctionSqlAstExpression;
import org.hibernate.sql.ast.spi.SqlSelection;
import org.hibernate.sql.ast.tree.expression.Expression;

View File

@ -38,7 +38,7 @@ import org.hibernate.query.results.TableGroupImpl;
import org.hibernate.query.spi.DomainQueryExecutionContext;
import org.hibernate.query.sqm.BinaryArithmeticOperator;
import org.hibernate.query.sqm.ComparisonOperator;
import org.hibernate.query.sqm.SortOrder;
import org.hibernate.query.SortOrder;
import org.hibernate.query.sqm.internal.DomainParameterXref;
import org.hibernate.query.sqm.internal.SqmJdbcExecutionContextAdapter;
import org.hibernate.query.sqm.internal.SqmUtil;

View File

@ -41,7 +41,7 @@ import org.hibernate.query.SemanticException;
import org.hibernate.query.results.TableGroupImpl;
import org.hibernate.query.spi.DomainQueryExecutionContext;
import org.hibernate.query.sqm.ComparisonOperator;
import org.hibernate.query.sqm.SortOrder;
import org.hibernate.query.SortOrder;
import org.hibernate.query.sqm.internal.DomainParameterXref;
import org.hibernate.query.sqm.internal.SqmUtil;
import org.hibernate.query.sqm.mutation.internal.MultiTableSqmMutationConverter;

View File

@ -134,7 +134,7 @@ import org.hibernate.query.sqm.ComparisonOperator;
import org.hibernate.query.sqm.DynamicInstantiationNature;
import org.hibernate.query.sqm.FetchClauseType;
import org.hibernate.query.sqm.InterpretationException;
import org.hibernate.query.sqm.SortOrder;
import org.hibernate.query.SortOrder;
import org.hibernate.query.sqm.SqmExpressible;
import org.hibernate.query.sqm.SqmPathSource;
import org.hibernate.query.sqm.SqmQuerySource;

View File

@ -15,8 +15,7 @@ import org.hibernate.query.criteria.JpaCteCriteria;
import org.hibernate.query.criteria.JpaCteCriteriaAttribute;
import org.hibernate.query.criteria.JpaCteCriteriaType;
import org.hibernate.query.criteria.JpaSearchOrder;
import org.hibernate.query.sqm.NullPrecedence;
import org.hibernate.query.sqm.SortOrder;
import org.hibernate.query.SortOrder;
import org.hibernate.query.sqm.tree.expression.SqmExpression;
import org.hibernate.query.sqm.tree.expression.SqmLiteral;
import org.hibernate.query.sqm.tree.select.SqmSelectQuery;
@ -28,11 +27,9 @@ import org.hibernate.query.sqm.NodeBuilder;
import org.hibernate.query.sqm.SemanticQueryWalker;
import org.hibernate.query.sqm.tree.AbstractSqmNode;
import org.hibernate.query.sqm.tree.SqmCopyContext;
import org.hibernate.query.sqm.tree.SqmStatement;
import org.hibernate.query.sqm.tree.SqmVisitableNode;
import jakarta.persistence.criteria.AbstractQuery;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Subquery;
/**

View File

@ -8,8 +8,8 @@ package org.hibernate.query.sqm.tree.cte;
import org.hibernate.query.criteria.JpaCteCriteriaAttribute;
import org.hibernate.query.criteria.JpaSearchOrder;
import org.hibernate.query.sqm.NullPrecedence;
import org.hibernate.query.sqm.SortOrder;
import org.hibernate.query.NullPrecedence;
import org.hibernate.query.SortOrder;
import org.hibernate.query.sqm.tree.SqmCopyContext;
/**

View File

@ -6,8 +6,8 @@
*/
package org.hibernate.query.sqm.tree.select;
import org.hibernate.query.sqm.NullPrecedence;
import org.hibernate.query.sqm.SortOrder;
import org.hibernate.query.NullPrecedence;
import org.hibernate.query.SortOrder;
import org.hibernate.query.criteria.JpaExpression;
import org.hibernate.query.criteria.JpaOrder;
import org.hibernate.query.sqm.tree.SqmCopyContext;

View File

@ -69,9 +69,9 @@ import org.hibernate.query.sqm.FetchClauseType;
import org.hibernate.query.sqm.FrameExclusion;
import org.hibernate.query.sqm.FrameKind;
import org.hibernate.query.sqm.FrameMode;
import org.hibernate.query.sqm.NullPrecedence;
import org.hibernate.query.NullPrecedence;
import org.hibernate.query.sqm.SetOperator;
import org.hibernate.query.sqm.SortOrder;
import org.hibernate.query.SortOrder;
import org.hibernate.query.sqm.UnaryArithmeticOperator;
import org.hibernate.query.sqm.function.AbstractSqmSelfRenderingFunctionDescriptor;
import org.hibernate.query.sqm.function.MultipatternSqmFunctionDescriptor;

View File

@ -6,8 +6,8 @@
*/
package org.hibernate.sql.ast.tree.cte;
import org.hibernate.query.sqm.NullPrecedence;
import org.hibernate.query.sqm.SortOrder;
import org.hibernate.query.NullPrecedence;
import org.hibernate.query.SortOrder;
/**
* @author Christian Beikov

View File

@ -6,8 +6,8 @@
*/
package org.hibernate.sql.ast.tree.select;
import org.hibernate.query.sqm.NullPrecedence;
import org.hibernate.query.sqm.SortOrder;
import org.hibernate.query.NullPrecedence;
import org.hibernate.query.SortOrder;
import org.hibernate.sql.ast.SqlAstWalker;
import org.hibernate.sql.ast.tree.SqlAstNode;
import org.hibernate.sql.ast.tree.expression.Expression;

View File

@ -12,7 +12,7 @@ import java.util.List;
import org.hibernate.Session;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.query.sqm.SortOrder;
import org.hibernate.query.SortOrder;
import org.hibernate.metamodel.mapping.PluralAttributeMapping;
import org.hibernate.metamodel.mapping.ordering.OrderByFragmentImpl;
import org.hibernate.metamodel.mapping.ordering.ast.OrderingSpecification;

View File

@ -19,8 +19,8 @@ import org.hibernate.query.criteria.JpaCrossJoin;
import org.hibernate.query.criteria.JpaExpression;
import org.hibernate.query.criteria.JpaRoot;
import org.hibernate.query.criteria.JpaWindow;
import org.hibernate.query.sqm.NullPrecedence;
import org.hibernate.query.sqm.SortOrder;
import org.hibernate.query.NullPrecedence;
import org.hibernate.query.SortOrder;
import org.hibernate.testing.orm.domain.StandardDomainModel;
import org.hibernate.testing.orm.domain.gambit.EntityOfBasics;

View File

@ -68,7 +68,7 @@ public class OrderTest {
assertEquals("Hibernate in Action", titlesAsc.get(1));
assertEquals("Java Persistence with Hibernate", titlesAsc.get(0));
titlesAsc = session.createSelectionQuery("from Book order by isbn asc", Book.class)
.unordered()
.clearOrder()
.ascending(title)
.getResultList()
.stream().map(book -> book.title)
@ -134,7 +134,7 @@ public class OrderTest {
assertEquals("Java Persistence with Hibernate", titlesAsc.get(0));
titlesAsc = session.createSelectionQuery("from Book where title like ?1 order by isbn asc", Book.class)
.setParameter(1, "%Hibernate%")
.unordered()
.clearOrder()
.ascending(title)
.getResultList()
.stream().map(book -> book.title)
@ -200,7 +200,7 @@ public class OrderTest {
assertEquals("Java Persistence with Hibernate", titlesAsc.get(0));
titlesAsc = session.createSelectionQuery("from Book where title like :title order by isbn asc", Book.class)
.setParameter("title", "%Hibernate%")
.unordered()
.clearOrder()
.ascending(title)
.getResultList()
.stream().map(book -> book.title)

View File

@ -13,9 +13,9 @@ import org.hibernate.query.sqm.tree.select.SqmSortSpecification;
import org.hibernate.testing.TestForIssue;
import org.junit.Test;
import static org.hibernate.query.sqm.NullPrecedence.FIRST;
import static org.hibernate.query.sqm.SortOrder.ASCENDING;
import static org.hibernate.query.sqm.SortOrder.DESCENDING;
import static org.hibernate.query.NullPrecedence.FIRST;
import static org.hibernate.query.SortOrder.ASCENDING;
import static org.hibernate.query.SortOrder.DESCENDING;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.mockito.Mockito.mock;