Sorting query results
In the course of this section, we will learn different techniques that can be used to sort query results with Spring Data JPA. We will also learn the guidelines that we can use to select a proper sorting method for each situation.
Sorting with method name
If we are building our queries by using the query generation from the method name strategy, we can sort query results by the following steps:
Creating a query method
Modifying an existing service method to use the new query method.
Creating the query method
When we are building our queries by using the query generation from the method name strategy, we can use the OrderBy
keyword to sort the query results when we:
Append the
OrderBy
keyword to the method name.Append the property expression that corresponds to the property of the entity, which is used to sort the query results, to the method name.
Append a keyword describing the sorting order to the method name. If the query results are sorted in ascending order, the keyword...