- 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<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.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Order<T> Defines a list ofSortcriteria, ordered from highest precedence to lowest precedence.booleanDetermines whether this instance specifies matchingSortcriteria in the same order of precedence as another instance.inthashCode()Computes a hash code for this instance.iterator()Returns an iterator that follows the order of precedence for theSortcriteria, from highest precedence to lowest.page(long pageNumber) Create aPageRequestfor the specified page number of page size 10 (the default forPageRequest) of the query results sorted according to any static sort criteria that is specified and the ordered list ofSortcriteria that is represented by this instance.pageSize(int size) Create aPageRequestfor the first page of the specified page size of the query results sorted according to any static sort criteria that is specified and the ordered list ofSortcriteria that is represented by this instance.sorts()The instances ofSortbelonging 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, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Method Details
-
by
Defines a list of
Sortcriteria, 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
-
equals
Determines whether this instance specifies matchingSortcriteria in the same order of precedence as another instance. -
hashCode
-
iterator
-
page
Create aPageRequestfor the specified page number of page size 10 (the default forPageRequest) of the query results sorted according to any static sort criteria that is specified and the ordered list ofSortcriteria that is represented by this instance.- Parameters:
pageNumber- requested page number.- Returns:
- a request for a page of results that are sorted based on the sort criteria represented by this instance
and with the specified page number. This method never returns
null.
-
pageSize
Create aPageRequestfor the first page of the specified page size of the query results sorted according to any static sort criteria that is specified and the ordered list ofSortcriteria that is represented by this instance.- Parameters:
size- requested size of pages.- Returns:
- a request for a page of results that are sorted based on the sort criteria represented by this instance
and with the specified page size. This method never returns
null.
-
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.
-