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.

  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> Order<T>
    by(Sort<? super T>... sorts)
    Defines a list of Sort criteria, ordered from highest precedence to lowest precedence.
    boolean
    equals(Object other)
    Determines whether this instance specifies matching Sort criteria in the same order of precedence as another instance.
    int
    Computes a hash code for this instance.
    Iterator<Sort<? super T>>
    Returns an iterator that follows the order of precedence for the Sort criteria, from highest precedence to lowest.
    page(long pageNumber)
    Create a PageRequest for the specified page number of page size 10 (the default for PageRequest) of the query results sorted according to any static sort criteria that is specified and the ordered list of Sort criteria that is represented by this instance.
    pageSize(int size)
    Create a PageRequest for 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 of Sort criteria that is represented by this instance.
    List<Sort<? super T>>
    The instances of Sort belonging to this Order.
    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.

    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

      @SafeVarargs public static <T> Order<T> by(Sort<? super T>... sorts)

      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

      public List<Sort<? super T>> sorts()
      The instances of Sort belonging to this Order.
      Returns:
      the instances of Sort, from highest precedence to lowest precedence.
    • equals

      public boolean equals(Object other)
      Determines whether this instance specifies matching Sort criteria in the same order of precedence as another instance.
      Overrides:
      equals in class Object
      Returns:
      true if the other instance is an Order that specifies the same ordering of sort criteria as this instance.
    • hashCode

      public int hashCode()
      Computes a hash code for this instance.
      Overrides:
      hashCode in class Object
      Returns:
      hash code.
    • iterator

      public Iterator<Sort<? super T>> iterator()
      Returns an iterator that follows the order of precedence for the Sort criteria, from highest precedence to lowest.
      Specified by:
      iterator in interface Iterable<T>
      Returns:
      iterator over the sort criteria.
    • page

      public PageRequest<T> page(long pageNumber)
      Create a PageRequest for the specified page number of page size 10 (the default for PageRequest) of the query results sorted according to any static sort criteria that is specified and the ordered list of Sort criteria 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

      public PageRequest<T> pageSize(int size)
      Create a PageRequest for 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 of Sort criteria 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

      public String 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.
      Overrides:
      toString in class Object
      Returns:
      textual representation of this instance.