Sorting in Spring Data JPA

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

In previous article we saw how query methods can be written.Now what if we want to sort results of query methods according to some criteria? This article will deal with that problem. We can sort results without writing any queries directly. We will see sorting in spring data jpa.

Sorting in Spring Data JPA:

We will continue from our previous query methods example. It is mandatory to read it before studying this post. Sorting in Spring data JPA can be achieved using Sort object and Order object.

Here is what javadoc says about them

Sort option for queries. You have to provide at least a list of properties to sort for that must not include null or empty strings. The direction defaults to DEFAULT_DIRECTION.
and Order

PropertyPath implements the pairing of an Sort.Direction and a property. It is used to provide input for Sort
Let us continue with our example,

Notice that findBySalaryGreaterThan(Long salary, Sort sort) is a query methods which accepts first argument as salary and second parameters as a Sort object. Whatever criteria we provide to Sort will be applied to query with salary greater than number passed as parameter. Let us try sorting in spring data jpa.

Note the code below

  • We are trying to create a new Sort object.
  • We are passing Sort.Order as parameter to Sort.
  • Sort.Order, also takes another parameter Direction, which can be ascending or descending.
  • Sort.Order takes another parameter “lastName” which means, results need to be sorted according to lastName.
  • So complete Sort object that we have created and passed to findBySalaryGreaterThan(new Long(10000), sort); means that it will return the results of all employees whose salary is greater than 10000 and those results will be filtered in ascending order of lastName  propertly.

Let us run this sorting in spring data jpa example and check output.

Employee [employeeId=360451, firstName=Nilesh, lastName=Deshmukh, salary=69000, department=null]
Employee [employeeId=360449, firstName=Pranil, lastName=Gilda, salary=52825, department=null]
Employee [employeeId=360450, firstName=Ajinkya, lastName=Joshi, salary=69000, department=null]
Employee [employeeId=360448, firstName=Prasad, lastName=Kharkar, salary=80000, department=null]

I hope this helped understand sorting in spring data jpa. In further articles we will see how to use pagination.

Share Button

Prasad Kharkar

Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

2 thoughts on “Sorting in Spring Data JPA

  • Pingback:Pagination in spring data jpa - theJavaGeek

  • March 1, 2018 at 3:36 pm

    Hello Prasad,

    What if I want the sort order on a complex type?

    Class Employee{
    public name;
    public List addressList;

    Class Address{
    String adLine1;
    String adLine2;

    Now when I do a query of findByName on employee, I want his address details to be sorted by adLine1? How do I do this?


Leave a Reply

Your email address will not be published. Required fields are marked *