Module jakarta.data

Annotation Interface OrderBy


@Repeatable(OrderBy.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 are supplied dynamically by Sort parameters, any Order parameter, or by a PageRequest parameter with sorting criteria.

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 PageRequest.sorts().

 @OrderBy("lastName")
 @OrderBy("firstName")
 Person[] findByZipCode(int zipCode, PageRequest<?> pageRequest);
 

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 with an @OrderBy annotation must not have:

  • the Query by Method Name OrderBy keyword in its name, nor
  • a @Query annotation specifying a JDQL or JPQL query with an ORDER BY clause.

A Jakarta Data provider is permitted to reject such a repository method declaration at compile time or to implement the method to throw UnsupportedOperationException.

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.