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 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 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.
    static <T> Order<T>
    by(List<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.
    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.
    • by

      public static <T> Order<T> by(List<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.
    • 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.