To paginate a native SQL query in JPA, pass a Pageable to a Spring Data repository method annotated with @Query(nativeQuery = true) and give it an explicit countQuery. Without Spring Data, call setFirstResult and setMaxResults on the Query returned by EntityManager.createNativeQuery.
Note that @NativeQuery is not part of the JPA specification. It is a Spring Data JPA convenience annotation in recent versions that is equivalent to @Query(nativeQuery = true); the older form works in every version, so the examples use it.
Declare the method with a Pageable parameter and tell Spring Data how to count:
Calling it:
Spring Data runs the native query with setFirstResult(offset) and setMaxResults(size). Hibernate then adds the right clause for the configured database dialect, such as limit ? offset ? on PostgreSQL and MySQL or offset ? rows fetch first ? rows only on SQL Server and Oracle.
Text blocks (the triple-quoted strings) need Java 15 or later, which every currently supported Spring Boot release satisfies.
For JPQL, Spring Data derives the count query by rewriting the query. It cannot reliably do that for arbitrary native SQL, so a Page return type without countQuery fails at startup or at runtime with an error about the count query. Provide one that:
from and where clauses,order by (it only slows the count),If you return Slice<T> or List<T>, no count query is run and countQuery is not needed.
The Sort inside a Pageable is limited for native queries. Spring Data cannot safely rewrite arbitrary SQL, and sorting by an entity property name does not map to a column name. Two safe options:
order by in the SQL and pass PageRequest.of(page, size) with no sort, as above.case expression.Never concatenate user-supplied sort text into SQL; that is an injection risk. Native query parameters (:department) are bound safely, but identifiers such as column names cannot be bound.
Native queries do not have to return entities. Use an interface projection with aliases that match the getter names:
Aliases are case-sensitive in some databases, so quote them or keep them in the case the database returns. Spring Data 3 and later also supports record-based DTO projections for many query types, but interface projections are the most dependable choice for native queries.
Without Spring Data, use createNativeQuery and the same two methods:
The count returns different Java types per database (Long, BigInteger, BigDecimal), so read it as Number.
When a native query exists for performance, offset paging often undermines it. Keyset pagination uses a where filter on the last seen key:
This works on indexed columns in constant time per page. For multi-column ordering, compare tuples, for example (hire_date, id) < (:lastDate, :lastId), where your database supports row-value comparison.
No. JPA defines @NamedNativeQuery and EntityManager.createNativeQuery. @NativeQuery is a Spring Data JPA shortcut in recent versions; @Query(nativeQuery = true) works in all of them.
Spring Data cannot derive a count query from arbitrary SQL. Add a countQuery or return Slice or List.
Only in limited cases. Write the order by in the SQL yourself and avoid passing user-controlled sort fields into it.
Yes from your code's perspective. Hibernate generates the dialect-specific limit, offset or fetch first syntax.
For native queries, add nativeQuery = true, a matching countQuery and a fixed order by with a unique tiebreaker. Use projections instead of entities when you only need a few columns, read counts as Number in plain JPA, and prefer keyset pagination for deep pages.