org.hibernate.query.Order Maven / Gradle / Ivy
Show all versions of beangle-hibernate-core Show documentation
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* 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;
import jakarta.persistence.metamodel.SingularAttribute;
import org.hibernate.Incubating;
import java.util.Objects;
/**
* A rule for sorting a query result set.
*
* 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 SelectionQuery#setOrder}.
*
* A parameter of a {@linkplain org.hibernate.annotations.processing.HQL
* HQL query method} may be declared with type {@code Order super E>},
* {@code List>}, or {@code Order super E>...} (varargs)
* where {@code E} is the entity type returned by the query.
*
* @param The result type of the query to be sorted
*
* @see SelectionQuery#setOrder(Order)
* @see SelectionQuery#setOrder(java.util.List)
*
* @author Gavin King
*
* @since 6.3
*/
@Incubating
public class Order {
private final SortDirection order;
private final SingularAttribute attribute;
private final Class entityClass;
private final String attributeName;
private final NullPrecedence nullPrecedence;
private final int element;
private Order(SortDirection order, NullPrecedence nullPrecedence, SingularAttribute attribute) {
this.order = order;
this.attribute = attribute;
this.attributeName = attribute.getName();
this.entityClass = attribute.getDeclaringType().getJavaType();
this.nullPrecedence = nullPrecedence;
this.element = 1;
}
private Order(SortDirection order, NullPrecedence nullPrecedence, Class entityClass, String attributeName) {
this.order = order;
this.entityClass = entityClass;
this.attributeName = attributeName;
this.attribute = null;
this.nullPrecedence = nullPrecedence;
this.element = 1;
}
private Order(SortDirection order, NullPrecedence nullPrecedence, int element) {
this.order = order;
this.entityClass = null;
this.attributeName = null;
this.attribute = null;
this.nullPrecedence = nullPrecedence;
this.element = element;
}
public static Order asc(SingularAttribute attribute) {
return new Order<>(SortDirection.ASCENDING, NullPrecedence.NONE, attribute);
}
public static Order desc(SingularAttribute attribute) {
return new Order<>(SortDirection.DESCENDING, NullPrecedence.NONE, attribute);
}
public static Order by(SingularAttribute attribute, SortDirection direction) {
return new Order<>(direction, NullPrecedence.NONE, attribute);
}
public static Order by(SingularAttribute attribute, SortDirection direction, NullPrecedence nullPrecedence) {
return new Order<>(direction, nullPrecedence, attribute);
}
public static Order asc(Class entityClass, String attributeName) {
return new Order<>( SortDirection.ASCENDING, NullPrecedence.NONE, entityClass, attributeName );
}
public static Order desc(Class entityClass, String attributeName) {
return new Order<>( SortDirection.DESCENDING, NullPrecedence.NONE, entityClass, attributeName );
}
public static Order by(Class entityClass, String attributeName, SortDirection direction) {
return new Order<>( direction, NullPrecedence.NONE, entityClass, attributeName );
}
public static Order by(Class entityClass, String attributeName, SortDirection direction, NullPrecedence nullPrecedence) {
return new Order<>( direction, nullPrecedence, entityClass, attributeName );
}
public static Order