- Type Parameters:
T
- entity class of the attributes that are used as sort criteria.
Requests sorting on various entity attributes.
A query method of a repository may have a parameter of type
Order
if its return type indicates that it may return
multiple entities. The parameter of type Order
must occur
after the method parameters representing regular parameters of
the query itself.
The Order
class is useful in combination with the
StaticMetamodel
for helping to enforce type safety of
sort criteria during development. For example,
Page<Employee>> findByYearHired(int year, PageRequest pageRequest, Order<Employee>); ... page1 = employees.findByYearHired(Year.now(), PageRequest.ofSize(10), Order.by(_Employee.salary.desc(), _Employee.lastName.asc(), _Employee.firstName.asc()));
The relative precedence of an instance of Sort
belonging
to an Order
is determined by its position within the
list of Sort
instances.
A repository method may declare static sorting criteria using
the OrderBy
keyword or @OrderBy
annotation,
and also accept dynamic sorting criteria via its parameters. In this
situation, the static sorting criteria are applied first, followed by
any dynamic sorting criteria specified by instances of Sort
.
In the example above, the matching employees are sorted first by salary from highest to lowest. Employees with the same salary are then sorted alphabetically by last name. Employees with the same salary and last name are then sorted alphabetically by first name.
A repository method may not be declared with more than one parameter
of type Order
.
A repository method throws DataException
if the database is incapable of ordering the query results using the given
sort criteria.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Order<T>
Defines a list ofSort
criteria, ordered from highest precedence to lowest precedence.static <T> Order<T>
Defines a list ofSort
criteria, ordered from highest precedence to lowest precedence.boolean
Determines whether this instance specifies matchingSort
criteria in the same order of precedence as another instance.int
hashCode()
Computes a hash code for this instance.iterator()
Returns an iterator that follows the order of precedence for theSort
criteria, from highest precedence to lowest.sorts()
The instances ofSort
belonging to thisOrder
.toString()
Textual representation of this instance, including the result of invokingSort.toString()
on each member of the sort criteria, in order of precedence from highest to lowest.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Method Details
-
by
Defines a list of
Sort
criteria, ordered from highest precedence to lowest precedence.- Type Parameters:
T
- entity class of the attributes that are used as sort criteria.- Parameters:
sorts
- sort criteria to use, ordered from highest precedence to lowest precedence.- Returns:
- a new instance indicating the order of precedence for sort criteria.
This method never returns
null
.
-
by
Defines a list of
Sort
criteria, ordered from highest precedence to lowest precedence.- Type Parameters:
T
- entity class of the attributes that are used as sort criteria.- Parameters:
sorts
- sort criteria to use, ordered from highest precedence to lowest precedence.- Returns:
- a new instance indicating the order of precedence for sort criteria.
This method never returns
null
.
-
sorts
The instances ofSort
belonging to thisOrder
.- Returns:
- the instances of
Sort
, from highest precedence to lowest precedence.
-
equals
Determines whether this instance specifies matchingSort
criteria in the same order of precedence as another instance. -
hashCode
public int hashCode()Computes a hash code for this instance. -
iterator
Returns an iterator that follows the order of precedence for theSort
criteria, from highest precedence to lowest. -
toString
Textual representation of this instance, including the result of invokingSort.toString()
on each member of the sort criteria, in order of precedence from highest to lowest.
-