Module jakarta.data
Package jakarta.data

Class Order<T>

java.lang.Object
jakarta.data.Order<T>
Type Parameters:
T - entity class of the attributes that are used as sort criteria.
All Implemented Interfaces:
Iterable<Sort<? super T>>

public class Order<T> extends Object implements Iterable<Sort<? super T>>

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<Employee> pageRequest);
 ...
 page1 = employees.findByYearHired(Year.now(),
                                   Order.by(_Employee.salary.desc(),
                                            _Employee.lastName.asc(),
                                            _Employee.firstName.asc())
                                        .page(1)
                                        .size(10));
 

When combined on a method with static sort criteria (OrderBy keyword or OrderBy annotation or Query with an ORDER BY clause), the static sort criteria are applied first, followed by the dynamic sort criteria that are defined by Sort instances in the order listed.

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 IllegalArgumentException if it is called with an argument of type Order and a separate argument of type PageRequest that has nonempty sort criteria.

A repository method throws DataException if the database is incapable of ordering the query results using the given sort criteria.