Close

Spring Data - Sorting with JpaSort

Following example shows how to use JpaSort to do sorting by applying JPQL function to a property.

Entity

@Entity
public class Employee {
  private @Id
  @GeneratedValue
  Long id;
  private String name;
  private String dept;
  private int salary;
    .............
}

Repository

package com.logicbig.example;

import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import java.util.List;

public interface EmployeeRepository extends CrudRepository<Employee, Long> {

  @Query("SELECT e FROM Employee e WHERE e.dept = ?1")
  public List<Employee> findByDepartment(String deptName, Sort sort);
}

Example Client

package com.logicbig.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.JpaSort;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;

@Component
public class ExampleClient {

  @Autowired
  private EmployeeRepository repo;

  public void run() {
      List<Employee> employees = createEmployees();
      repo.saveAll(employees);

      System.out.println(" -- finding all employees --");
      Iterable<Employee> all = repo.findAll();
      all.forEach(System.out::println);

      System.out.println(" -- finding by dept Sales sort by 'salary' and 'name length'  --");
      List<Employee> list = repo.findByDepartment("Sales", JpaSort.unsafe("LENGTH(name)", "salary"));
      list.forEach(System.out::println);
  }

  private List<Employee> createEmployees() {
      return Arrays.asList(
              Employee.create("Tina", "Sales", 3000),
              Employee.create("Mike", "Sales", 1000),
              Employee.create("Rose", "IT", 4000),
              Employee.create("Tanaka", "Sales", 3000),
              Employee.create("Abbie", "Sales", 3000),
              Employee.create("Charlie", "IT", 2500)
      );
  }
}

Main class

public class ExampleMain {

  public static void main(String[] args) {
      AnnotationConfigApplicationContext context =
              new AnnotationConfigApplicationContext(AppConfig.class);
      ExampleClient exampleClient = context.getBean(ExampleClient.class);
      exampleClient.run();
      EntityManagerFactory emf = context.getBean(EntityManagerFactory.class);
      emf.close();
  }
}
 -- finding all employees --
Employee{id=1, name='Tina', dept='Sales', salary=3000}
Employee{id=2, name='Mike', dept='Sales', salary=1000}
Employee{id=3, name='Rose', dept='IT', salary=4000}
Employee{id=4, name='Tanaka', dept='Sales', salary=3000}
Employee{id=5, name='Abbie', dept='Sales', salary=3000}
Employee{id=6, name='Charlie', dept='IT', salary=2500}
-- finding by dept Sales sort by 'salary' and 'name length' --
Employee{id=2, name='Mike', dept='Sales', salary=1000}
Employee{id=1, name='Tina', dept='Sales', salary=3000}
Employee{id=5, name='Abbie', dept='Sales', salary=3000}
Employee{id=4, name='Tanaka', dept='Sales', salary=3000}

Example Project

Dependencies and Technologies Used:

  • spring-data-jpa 2.0.7.RELEASE: Spring Data module for JPA repositories.
    Uses org.springframework:spring-context version 5.0.6.RELEASE
  • hibernate-core 5.3.1.Final: Hibernate's core ORM functionality.
    Implements javax.persistence:javax.persistence-api version 2.2
  • h2 1.4.197: H2 Database Engine.
  • JDK 1.8
  • Maven 3.3.9

spring-data-jpa-sort-example Select All Download
  • spring-data-jpa-sort-example
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • ExampleClient.java
          • resources
            • META-INF

    See Also