PageRequest is the standard implementation of the Pageable interface in Spring Data. It is an immutable object holding a zero-based page index, a page size and an optional Sort, and you pass it to repository methods to fetch one sorted page of results.
You create it with static factory methods such as PageRequest.of(page, size). The older public constructors were deprecated and removed in favor of these factories.
PageRequest lives in org.springframework.data.domain. The factory methods are:
| Factory | Meaning |
|---|---|
PageRequest.of(page, size) | Page and size, no sorting |
PageRequest.of(page, size, Sort) | Page, size and a Sort object |
PageRequest.of(page, size, Sort.Direction, String... properties) | Page, size, one direction, one or more properties |
PageRequest.ofSize(size) | First page with the given size (Spring Data 2.5+) |
The page index is zero-based, so 0 is the first page. The page size must be at least 1, and a negative page index throws IllegalArgumentException.
For a JPA repository, PageRequest.of(2, 10, Sort.by("title")) produces a query equivalent to:
The offset is page index times page size. Spring Data also runs a count query when the return type is Page, so it can report total elements and pages. The exact syntax depends on the database dialect.
Sort names refer to entity properties. Add a unique property such as the id as the last order so rows keep a stable position between pages.
PageRequest is immutable, and its methods return new instances:
A common loop processes a large table in batches:
nextPageable() returns Pageable.unpaged() on the last page, so checking hasNext() ends the loop. For updates while iterating, deep offsets and changing data can skip rows, so prefer keyset scrolling for big batch jobs.
You rarely build PageRequest by hand in controllers. Declare a Pageable parameter and Spring MVC creates a PageRequest from page, size and sort parameters. Use @PageableDefault for defaults, and build a PageRequest manually in services, scheduled jobs and tests.
A @DataJpaTest is enough to verify paging behavior against an in-memory or containerized database:
The last page would contain only 5 items, which is a useful edge case to assert as well.
Keep the size within what your interface shows, usually 10 to 50, and enforce a maximum on the server. Always sort by a column with an index, since the database must order the whole filtered set before applying the offset.
No. Pageable is the interface and PageRequest is its common, immutable implementation. You normally pass it where a Pageable is expected.
The constructors are not public in current Spring Data versions. Use PageRequest.of(...) instead.
Use Pageable.unpaged(). Be careful with large tables, because it removes the limit.
Not at creation time. An unknown property fails when the query is built, with a PropertyReferenceException.
PageRequest bundles page index, size and sort into an immutable Pageable. Create it with PageRequest.of, remember that pages start at 0, add a stable tie-breaker to sorts, and use next() or nextPageable() to walk through results.