Close

Spring Data JPA - Pagination using Pageable parameter

[Last Updated: May 31, 2018]

Spring Data JPA allows query methods to have a special parameter Pageable to apply pagination. For example:

      public List<Employee> findByDept(String deptName, Pageable pageable)

Pageable is an interface which contains requested page information. We can use it's implementation PageRequest which has various factory methods:

 PageRequest of(int page, int size)

Where: page - zero-based page index.
size - the size of the page to be returned.

Example:

      List<Employee> list = repo.findByDept("Sales", PageRequest.of(0, 5));

Pagination with sorting

We can use following method of PageRequest to apply sorting as well:

public static PageRequest of(int page,  int size,   Sort sort)

Example:

 List<Employee> list = repo.findByDept("Sales", PageRequest.of(0, 5, Sort.by("salary")));


Pagination can be applied to both query methodsor declared queries by using @Query.

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.domain.Pageable;
import org.springframework.data.repository.CrudRepository;
import java.util.List;

public interface EmployeeRepository extends CrudRepository<Employee, Long> {

  public List<Employee> findByDept(String deptName, Pageable pageable);
}

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 --");
      Iterable<Employee> all = repo.findAll();
      all.forEach(System.out::println);

      System.out.println(" -- paginating where dept is Sales --");
      long count = repo.count();
      int pageSize = 2;
      long pages = count / pageSize;
      for (int i = 0; i < pages; i++) {
          System.out.printf("page num: %s%n", i);
          List<Employee> list = repo.findByDept("Sales", PageRequest.of(i, pageSize));
          list.forEach(System.out::println);
      }

      System.out.println(" -- paginating with sorting by salaries` where dept is Sales --");
      for (int i = 0; i < pages; i++) {
          System.out.printf("page num: %s%n", i);
          List<Employee> list = repo.findByDept("Sales",
                  PageRequest.of(i, pageSize, Sort.by("salary").descending()));
          list.forEach(System.out::println);
      }
  }

  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)
      );
  }
}

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='Diana', dept='Sales', salary=2000}
Employee{id=2, name='Mike', dept='Sales', salary=1000}
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}
Employee{id=6, name='Charlie', dept='Sales', salary=2500}
Employee{id=7, name='Jim', dept='Sales', salary=4500}
Employee{id=8, name='Sam', dept='Sales', salary=2500}
-- paginating where dept is Sales --
page num: 0
Employee{id=1, name='Diana', dept='Sales', salary=2000}
Employee{id=2, name='Mike', dept='Sales', salary=1000}
page num: 1
Employee{id=4, name='Sara', dept='Sales', salary=3000}
Employee{id=5, name='Andy', dept='Sales', salary=3000}
page num: 2
Employee{id=6, name='Charlie', dept='Sales', salary=2500}
Employee{id=7, name='Jim', dept='Sales', salary=4500}
page num: 3
Employee{id=8, name='Sam', dept='Sales', salary=2500}
-- paginating with sorting by salaries` where dept is Sales --
page num: 0
Employee{id=7, name='Jim', dept='Sales', salary=4500}
Employee{id=5, name='Andy', dept='Sales', salary=3000}
page num: 1
Employee{id=5, name='Andy', dept='Sales', salary=3000}
Employee{id=6, name='Charlie', dept='Sales', salary=2500}
page num: 2
Employee{id=6, name='Charlie', dept='Sales', salary=2500}
Employee{id=1, name='Diana', dept='Sales', salary=2000}
page num: 3
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

Pagination using Pageable Select All Download
  • spring-data-jpa-pagination
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • EmployeeRepository.java
          • resources
            • META-INF

    See Also