Close

Spring Data JPA - Extending PagingAndSortingRepository

[Updated: Jun 17, 2018, Created: May 31, 2018]

By extending PagingAndSortingRepository interface to define our repository, we can use the methods related to sorting and pagination defined by this interface. This interface is a sub-interface of CrudRepository. It has following methods:

package org.springframework.data.repository;
 .....
@NoRepositoryBean
public interface PagingAndSortingRepository<T, ID> extends CrudRepository<T, ID> {
	Iterable<T> findAll(Sort sort);
	Page<T> findAll(Pageable pageable);
}

Example

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.repository.PagingAndSortingRepository;

public interface EmployeeRepository extends PagingAndSortingRepository<Employee, Long> {

}

Example client

@Component
public class ExampleClient {

  @Autowired
  private EmployeeRepository repo;

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

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

      System.out.println(" -- paginate employees sorted by salaries  --");
      Pageable pageable = PageRequest.of(0, 3,
              Sort.by("salary").descending());

      while(true){
          Page<Employee> page = repo.findAll(pageable);
          System.out.println("Page no: "+page.getNumber());
          page.getContent().forEach(System.out::println);
          if(!page.hasNext()){
              break;
          }
          pageable = page.nextPageable();
      }
  }

  private List<Employee> createEmployees() {
      return Arrays.asList(
              Employee.create("Diana", "Sales", 2000),
              Employee.create("Mike", "Sales", 1000),
              Employee.create("Rose", "IT", 4000),
              Employee.create("Sara", "Sales", 3000),
              Employee.create("Andy", "Sales", 3000),
              Employee.create("Charlie", "Sales", 2500),
              Employee.create("Jim", "Sales", 4500),
              Employee.create("Sam", "Sales", 2500),
              Employee.create("Adam", "Sales", 5000),
              Employee.create("Tanaka", "Sales", 5500),
              Employee.create("Joe", "Sales", 1500)
      );
  }
}

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 sorted by names --
Employee{id=9, name='Adam', dept='Sales', salary=5000}
Employee{id=5, name='Andy', dept='Sales', salary=3000}
Employee{id=6, name='Charlie', dept='Sales', salary=2500}
Employee{id=1, name='Diana', dept='Sales', salary=2000}
Employee{id=7, name='Jim', dept='Sales', salary=4500}
Employee{id=11, name='Joe', dept='Sales', salary=1500}
Employee{id=2, name='Mike', dept='Sales', salary=1000}
Employee{id=3, name='Rose', dept='IT', salary=4000}
Employee{id=8, name='Sam', dept='Sales', salary=2500}
Employee{id=4, name='Sara', dept='Sales', salary=3000}
Employee{id=10, name='Tanaka', dept='Sales', salary=5500}
-- paginate employees sorted by salaries --
Page no: 0
Employee{id=10, name='Tanaka', dept='Sales', salary=5500}
Employee{id=9, name='Adam', dept='Sales', salary=5000}
Employee{id=7, name='Jim', dept='Sales', salary=4500}
Page no: 1
Employee{id=3, name='Rose', dept='IT', salary=4000}
Employee{id=4, name='Sara', dept='Sales', salary=3000}
Employee{id=5, name='Andy', dept='Sales', salary=3000}
Page no: 2
Employee{id=8, name='Sam', dept='Sales', salary=2500}
Employee{id=6, name='Charlie', dept='Sales', salary=2500}
Employee{id=1, name='Diana', dept='Sales', salary=2000}
Page no: 3
Employee{id=11, name='Joe', dept='Sales', salary=1500}
Employee{id=2, name='Mike', dept='Sales', salary=1000}

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

PagingAndSortingRepository Example Select All Download
  • spring-data-jpa-paging-and-sorting-repository
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • EmployeeRepository.java
          • resources
            • META-INF

    See Also