Annotation Interface OrderBy


@Repeatable(List.class) @Retention(RUNTIME) @Target(METHOD) public @interface OrderBy

Annotates a repository method to request sorting of results.

When multiple OrderBy annotations are specified on a repository method, the precedence for sorting follows the order in which the OrderBy annotations are specified, and after that follows any sort criteria that is supplied dynamically by Sort parameters or by a Pageable parameter with Pageable.sorts().

For example, the following sorts first by the lastName attribute in ascending order, and secondly, for entities with the same lastName, it then sorts by the firstName attribute, also in ascending order. For entities with the same lastName and firstName, it then sorts by criteria that is specified in the Pageable.sorts().

 @OrderBy("lastName")
 @OrderBy("firstName")
 Person[] findByZipCode(int zipCode, Pageable pagination);
 

The precise meaning of ascending and descending order is defined by the database, but generally ascending order for numeric values means smaller numbers before larger numbers and for string values means A before Z.

A repository method will fail if an OrderBy annotation is specified in combination with any of:

  • an OrderBy keyword
  • a Query annotation that contains an ORDER BY clause.

A repository method will fail with a DataException or a more specific subclass if the database is incapable of ordering with the requested sort criteria.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static @interface 
    Enables multiple OrderBy annotations on the same type.
  • Required Element Summary

    Required Elements
    Modifier and Type
    Required Element
    Description
    Entity attribute name to sort by.
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    boolean
    Indicate whether to use descending order when sorting by this attribute.
    boolean
    Indicates whether or not to request case insensitive ordering from a database with case sensitive collation.
  • Element Details

    • descending

      boolean descending

      Indicate whether to use descending order when sorting by this attribute.

      The default value of false means ascending sort.

      Returns:
      whether to use descending (versus ascending) order.
      Default:
      false
    • ignoreCase

      boolean ignoreCase

      Indicates whether or not to request case insensitive ordering from a database with case sensitive collation. A database with case insensitive collation performs case insensitive ordering regardless of the requested ignoreCase value.

      The default value is false.

      Returns:
      whether or not to request case insensitive sorting for the property.
      Default:
      false
    • value

      String value

      Entity attribute name to sort by.

      For example,

       @OrderBy("age")
       Stream<Person> findByLastName(String lastName);
       
      Returns:
      entity attribute name.