Hibernate Pagination Limits with Collection Join Fetches
29K reputation · 24 May 2022, 23:09 UTC
Pagination Behavior in Hibernate
Hibernate provides setFirstResult() and setMaxResults() to handle dataset pagination, delegating the specific SQL syntax to the configured Dialect. This mechanism is intended to restrict the number of rows returned by the database to optimize memory and network usage.
In-Memory Pagination Constraints
A known behavior occurs when pagination is combined with join fetches on @OneToMany or @ManyToMany collections. Because the resulting Cartesian product can distort the row count, Hibernate may shift the pagination process from the database level to the application level.
This shift is typically indicated by the HHH000104 warning, signifying that the entire result set is loaded into memory before the specified limits are applied. This creates a significant risk of OutOfMemoryError when dealing with large datasets.
- How can the application maintain database-level pagination while still retrieving associated collections?
- What are the performance implications of deep paging (high offset values) compared to keyset pagination in this context?
1 answer
0 question comments
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.