setFirstResult ordering boundary without explicit ORDER BY in Hibernate
0 reputation · 20 Oct 2020, 11:40 UTC
Hibernate's setFirstResult and setMaxResults pagination mechanism scans and discards database rows, resulting in O(n) cost for large offsets, a behavior consistent across Hibernate 5.x and 6.x when using standard JDBC. A documented requirement states that explicit ORDER BY is mandatory before pagination to ensure deterministic row ordering; omission yields non-deterministic results under typical database isolation levels. The practical boundary between verified deterministic ordering and isolation-level-dependent non-determinism remains unspecified in official guidance, particularly when second-level cache configurations or version-specific dialect optimizations are involved.
- Does the omission of ORDER BY guarantee non-deterministic results across all supported database dialects when setFirstResult exceeds the natural index scan threshold?
- How does second-level cache state interact with re-executed pagination queries after a Hibernate version upgrade?
- What is the minimum ORDER BY expression required to establish stable row ordering when setFirstResult and setMaxResults are combined with native database fetch optimizations?