Top Description Fields Constructors Methods
jakarta.data

public Class Order<T>

extends Object
implements Iterable<Sort<? super T>>
Class Inheritance
All Implemented Interfaces
java.lang.Iterable
Type Parameters
<T>
entity class of the attributes that are used as sort criteria.
Imports
java.util.Iterator, .List, jakarta.data.metamodel.StaticMetamodel, jakarta.data.repository.OrderBy

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.data.exceptions.DataException if the database is incapable of ordering the query results using the given sort criteria.

Field Summary

Modifier and TypeField and Description
private final List<Sort<? super T>>
sorts

Unmodifiable list of Sort instances, from highest precedence to lowest.

Constructor Summary

AccessConstructor and Description
private
Order(List<Sort<? super T>>
unmodifiable list of Sort instances, from highest precedence to lowest.
sorts
)

Creates a new instance.

Method Summary

Modifier and TypeMethod and Description
public static <
entity class of the attributes that are used as sort criteria.
T
>
Order<T>

Returns:

a new instance indicating the order of precedence for sort criteria. This method never returns null.
by
(Sort<? super T>...
sort criteria to use, ordered from highest precedence to lowest precedence.
sorts
)

Defines a list of Sort criteria, ordered from highest precedence to lowest precedence.

public static <
entity class of the attributes that are used as sort criteria.
T
>
Order<T>

Returns:

a new instance indicating the order of precedence for sort criteria. This method never returns null.
by
(List<Sort<? super T>>
sort criteria to use, ordered from highest precedence to lowest precedence.
sorts
)

Defines a list of Sort criteria, ordered from highest precedence to lowest precedence.

public boolean

Returns:

true if the other instance is an Order that specifies the same ordering of sort criteria as this instance.
equals
(Object
the reference object with which to compare.
other
)

Overrides java.lang.Object.equals.

Determines whether this instance specifies matching Sort criteria in the same order of precedence as another instance.

public int

Returns:

hash code.
hashCode
()

Overrides java.lang.Object.hashCode.

Computes a hash code for this instance.

public Iterator<Sort<? super T>>

Returns:

iterator over the sort criteria.
iterator
()

Implements java.lang.Iterable.iterator.

Returns an iterator that follows the order of precedence for the Sort criteria, from highest precedence to lowest.

public List<Sort<? super T>>

Returns:

the instances of Sort, from highest precedence to lowest precedence.
sorts
()

The instances of Sort belonging to this Order.

public String

Returns:

textual representation of this instance.
toString
()

Overrides java.lang.Object.toString.

Textual representation of this instance, including the result of invoking Sort#toString() on each member of the sort criteria, in order of precedence from highest to lowest.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

sortsback to summary
private final List<Sort<? super T>> sorts

Unmodifiable list of Sort instances, from highest precedence to lowest.

Constructor Detail

Orderback to summary
private Order(List<Sort<? super T>> sorts)

Creates a new instance.

Parameters
sorts:List<Sort<? super T>>

unmodifiable list of Sort instances, from highest precedence to lowest.

Method Detail

byback to summary
public static <T> Order<T> by(Sort<? super T>... sorts)

Defines a list of Sort criteria, ordered from highest precedence to lowest precedence.

Parameters
<T>
entity class of the attributes that are used as sort criteria.
sorts:Sort<? super T>[]

sort criteria to use, ordered from highest precedence to lowest precedence.

Returns:Order<T>

a new instance indicating the order of precedence for sort criteria. This method never returns null.

Annotations
@SafeVarargs
byback to summary
public static <T> Order<T> by(List<Sort<? super T>> sorts)

Defines a list of Sort criteria, ordered from highest precedence to lowest precedence.

Parameters
<T>
entity class of the attributes that are used as sort criteria.
sorts:List<Sort<? super T>>

sort criteria to use, ordered from highest precedence to lowest precedence.

Returns:Order<T>

a new instance indicating the order of precedence for sort criteria. This method never returns null.

equalsback to summary
public boolean equals(Object other)

Overrides java.lang.Object.equals.

Determines whether this instance specifies matching Sort criteria in the same order of precedence as another instance.

Parameters
other:Object

Doc from java.lang.Object.equals.

the reference object with which to compare.

Returns:boolean

true if the other instance is an Order that specifies the same ordering of sort criteria as this instance.

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Computes a hash code for this instance.

Returns:int

hash code.

Annotations
@Override
iteratorback to summary
public Iterator<Sort<? super T>> iterator()

Implements java.lang.Iterable.iterator.

Returns an iterator that follows the order of precedence for the Sort criteria, from highest precedence to lowest.

Returns:Iterator<Sort<? super T>>

iterator over the sort criteria.

Annotations
@Override
sortsback to summary
public List<Sort<? super T>> sorts()

The instances of Sort belonging to this Order.

Returns:List<Sort<? super T>>

the instances of Sort, from highest precedence to lowest precedence.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Textual representation of this instance, including the result of invoking Sort#toString() on each member of the sort criteria, in order of precedence from highest to lowest.

Returns:String

textual representation of this instance.

Annotations
@Override