Pagination in Spring Data Repositories

Hrishi D
2 min readJan 21, 2024

--

Spring Data JPA Pagination

Pagination is a technique used to break down a large set of data into smaller, more manageable chunks or pages. In the context of Spring Data, pagination is commonly used when dealing with databases to retrieve a subset of results at a time.

Key Concepts:

  1. Pageable:
  • Pageable is an interface in Spring Data that represents the information about pagination and sorting.
  • It includes details such as the page number, page size, sorting criteria, etc.

2. Page:

  • The Page interface represents a page of results, providing information about the current page’s content, total pages, total elements, and more.

JPARepository vs. PagingAndSortingRepository:

  1. JPARepository:
  • JpaRepository is an extension of PagingAndSortingRepository with additional JPA-specific features.
  • Provides methods for common CRUD operations and advanced querying capabilities.
  • Suitable for applications using the Java Persistence API (JPA) for data access.Implementing Pagination in Spring Data Repositories:
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

public interface YourEntityRepository extends JpaRepository<YourEntity, Long> {
// JpaRepository provides methods for paging and sorting
Page<YourEntity> findAll(Pageable pageable);
// Custom query method with pagination
Page<YourEntity> findByAgeGreaterThan(int age, Pageable pageable);
}

2. PagingAndSortingRepository:

  • PagingAndSortingRepository is a more generic interface providing basic paging and sorting functionality.
  • Suitable for applications focused on standard CRUD operations with support for pagination and sorting.
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

public interface YourEntityRepository extends PagingAndSortingRepository<YourEntity, Long> {
// PagingAndSortingRepository provides methods for paging and sorting
Page<YourEntity> findAll(Pageable pageable);
// Custom query method with pagination
Page<YourEntity> findByAgeGreaterThan(int age, Pageable pageable);
}

Additional Considerations:

1. Sorting:

  • Pagination often goes hand-in-hand with sorting. You can include sorting criteria in your Pageable object.
import org.springframework.data.repository.JpaRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

public interface YourEntityRepository extends JpaRepository<YourEntity, Long> {
// 1. Without custom query method with sorting by name
Page<YourEntity> findAllByAgeGreaterThanOrderByName(int age, Pageable pageable);
// 2. Custom query method with sorting by name
@Query("SELECT e FROM YourEntity e WHERE e.age > :age ORDER BY e.name")
Page<YourEntity> findAllByAgeGreaterThanSortedByName(@Param("age") int age, Pageable pageable);
}
  • The 1st findAllByAgeGreaterThanSortedByName method will sort the results by the name field in ascending order.

You can customise the method name by using the field name you want to sort by and appending OrderBy followed by the field name. The direction of sorting (ascending or descending) is determined by the method name. If you want descending order, you can use OrderBy…Desc instead.

2. Customising Page Size:

  • Customise the default page size by configuring it in your application properties.
spring:
data:
web:
pageable:
default-page-size: 10

3. Accessing Page Information:

  • After executing a paginated query, you can access information about the page, such as total pages and total elements.
Page<YourEntity> page = yourEntityRepository.findAll(PageRequest.of(pageNumber, pageSize));

int totalPages = page.getTotalPages();
long totalElements = page.getTotalElements();
List<YourEntity> content = page.getContent();

--

--