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 jakarta.
if the database is incapable of ordering the query results using the given
sort criteria.
| Modifier and Type | Field and Description |
|---|---|
| private final List | sorts
Unmodifiable list of Sort instances, from highest precedence to lowest. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static < entity class of the attributes that are used as sort criteria. T> Order | |
| public static < entity class of the attributes that are used as sort criteria. T> Order | Returns: a new instance indicating the order of precedence for sort criteria. This method never returnsnull.sort criteria to use, ordered from highest precedence to lowest precedence. sorts)Defines a list of |
| public boolean | Returns: true if the other instance is anOrder that specifies
the same ordering of sort criteria as this instance.the reference object with which to compare. other)Overrides java. Determines whether this instance specifies matching |
| public int | Returns: hash code.Overrides java. Computes a hash code for this instance. |
| public Iterator | Returns: iterator over the sort criteria.Implements java. Returns an iterator that follows the order of precedence for the
|
| public List | |
| public String | Returns: textual representation of this instance.Overrides java. Textual representation of this instance, including the result of invoking
|
| sorts | back to summary |
|---|---|
| private final List<Sort<? super T>> sorts Unmodifiable list of Sort instances, from highest precedence to lowest. | |
| Order | back to summary |
|---|---|
| private Order(List<Sort<? super T>> sorts) Creates a new instance. | |
| by | back to summary |
|---|---|
| public static <T> Order Defines a list of
| |
| by | back to summary |
|---|---|
| public static <T> Order Defines a list of | |
| equals | back to summary |
|---|---|
| public boolean equals(Object other) Overrides java. Determines whether this instance specifies matching
| |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. Computes a hash code for this instance.
| |
| iterator | back to summary |
|---|---|
| public Iterator Implements java. Returns an iterator that follows the order of precedence for the
| |
| sorts | back to summary |
|---|---|
| public List The instances of | |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Textual representation of this instance, including the result of invoking
| |