Does Hibernate 6 offer a native keyset pagination API compatible with Spring Data JPA repositories?
0 reputation · 23 Sept 2021, 03:59 UTC
Goal
Implement pagination over a table with millions of rows while avoiding the performance degradation of OFFSET‑based queries and still integrating with Spring Data JPA repository methods.
Constraints and uncertainty
Hibernate 6 supports scrolling and custom SQL fragments that can express a seek‑method (WHERE id > :lastId ORDER BY id LIMIT :size), but there is no first‑class KeysetPageable in Spring Data JPA as of the latest release. The application already uses Pageable‑based repository signatures, and changing them would affect many callers. It is unclear whether Hibernate’s low‑level APIs can be wrapped to satisfy the Pageable contract without losing the ability to request a total count or to support arbitrary sort orders.
Open questions
- Can Hibernate 6’s ScrollableResults or a custom SQL fragment be adapted to work with Spring Data’s Pageable interface so existing repository methods remain unchanged?
- Is it feasible to combine keyset pagination with an approximate total‑count query (e.g., using pg_class.reltuples) to satisfy UI pagination controls?
- What are the limitations regarding multi‑column sorting, bidirectional navigation, and transaction scope when using a keyset‑based approach with Hibernate?