← Back to list

Understanding Sorting & Pagination in Spring Data JPA

Most developers use sorting and pagination in Spring Data JPA daily.

Raushan Kumar · 2026-05-14 01:31 · 0 claps · 2.4 min read
#spring-data-jpa #spring-boot #database #sorting #pagination
Open on Medium ↗

Understanding Sorting & Pagination in Spring Data JPA

Most developers use sorting and pagination in Spring Data JPA daily.

Methods like:

  • Sort.by(...)
  • PageRequest.of(...)
  • findByTitle(String title, Pageable pageable)

look simple from the outside.

But internally, Spring Data JPA treats them more like:

  • metadata instructions
  • query configuration objects
  • dynamic query generation inputs

rather than just normal Java objects and methods.

In this article, we will explore:

  • how sorting works
  • how pagination metadata flows through the system
  • difference between Page, Pageable, and PageRequest
  • how Spring Data JPA converts pagination metadata into executable queries

i) Sorting in Spring Data JPA

Spring Data JPA provides sorting mainly using Sort class.

Example:

Sort.by("price")
Sort.by(Sort.Order.asc("price"))
Sort.by(Sort.Order.desc("price"))
  • field name → sorting target
  • ascending/descending → sorting direction metadata

Internally: Spring Data JPA stores this sorting information as query metadata and later converts it into SQL ordering instructions.

Example:

Sort.by(Sort.Order.desc("price"))

internally contributes toward SQL generation similar to:

ORDER BY price DESC

ii) Repository Method-Based Sorting

Sorting can also be declared directly in repository method naming.

Example:

findByNameOrderByPriceAsc()
findByNameOrderByPriceDesc()

Here:

  • OrderBy → sorting keyword
  • Price → sorting field
  • Asc/Desc → sorting direction

Spring Data JPA parses this repository method name and dynamically derives sorting query metadata internally.

In this approach:

  • separate the Sort object may not be required in the service layer
  • sorting logic becomes part of the repository query definition itself

iii) Pagination Core Components

Spring Data JPA pagination mainly revolves around:

Page
Pageable
PageRequest

1. Page

Page<T>

Represents:

  • paginated response/result

Contains:

  • current page data
  • total pages
  • total records
  • page metadata

Example:

Page<Product>

2. Pageable

Pageable

is an interface.

It acts like:

  • pagination metadata contract
  • pagination configuration abstraction

It carries:

  • page number
  • page size
  • sorting configuration

A developer usually interacts with this abstraction layer.

3. PageRequest

PageRequest

is the concrete implementation of:

Pageable

Used to create actual pagination request objects.

Example:

PageRequest.of(pageNumber, pageSize)
PageRequest.of(pageNumber, pageSize, sort)

iv) Real Pagination Implementation Flow

Suppose we define repository method like:

Page<Product> findByTitle(
        String title,
        Pageable pageable
);

Important understanding:

Pageable pageable

The parameter is what enables dynamic pagination support for this repository method.

Without this parameter:

  • Spring Data JPA cannot dynamically apply pagination metadata for that query.

v) Service Layer Flow

In the service layer:

# service layer
Pageable pageable =
        PageRequest.of(
                pageNumber,
                pageSize,
                Sort.by("title").ascending()
        );

Page<Product> result =
        productRepository.findByTitle(title, pageable);

Here:

  • PageRequest → creates a concrete pagination metadata object
  • Pageable → carries metadata abstraction
  • repository method → receives pagination instructions dynamically

vi) Internal Orchestration Flow

Developer
   ↓
PageRequest.of(...)
   ↓
Pageable metadata object created
   ↓
Repository method invoked
   ↓
Spring Data JPA parses Pageable metadata
   ↓
Pagination + Sorting query metadata generated
   ↓
Hibernate ORM
   ↓
SQL with LIMIT / OFFSET / ORDER BY generated
   ↓
JDBC
   ↓
Database
   ↓
Page result returned

vii) What Hibernate Does Internally

After Spring Data JPA prepares pagination metadata: Hibernate ORM takes over persistence orchestration responsibilities.

Hibernate internally:

  • applies entity mapping
  • converts ORM query into SQL
  • generates:LIMIT, OFFSET, ORDER BY
  • executes query through JDBC

Example generated SQL may look similar to:

SELECT *
FROM products
WHERE title = ?
ORDER BY title ASC
LIMIT 10
OFFSET 20

viii) Most Important Internal Understanding

Sorting and pagination in Spring Data JPA are mostly:

  • metadata-driven query orchestration systems

where:

  • Sort → carries sorting metadata
  • Pageable → carries pagination metadata
  • Spring Data JPA → parses metadata and generates query structure
  • Hibernate → performs ORM execution and SQL generation

If you found this helpful, do support with your reactions and share your thoughts in the comments.

SpringBoot #SpringDataJpa


메타데이터
post_id
f44b88ee4568
slug
understanding-sorting-pagination-in-spring-data-jpa-f44b88ee4568
url
https://medium.com/@raushan1156/understanding-sorting-pagination-in-spring-data-jpa-f44b88ee4568
canonical_url
https://medium.com/@raushan1156/understanding-sorting-pagination-in-spring-data-jpa-f44b88ee4568
author_url
https://medium.com/@raushan1156
status
ok
fetched_at
2026-06-14 13:58:26