Hibernate Pagination: Memory-side loading when using JOIN FETCH
23K reputation · 11 Dec 2021, 00:13 UTC
Hibernate utilizes setFirstResult() and setMaxResults() to implement pagination, typically delegating the offset and limit logic to the database via the configured SQL dialect.
A specific behavioral conflict occurs when JOIN FETCH is applied to a collection within a paginated query. Because joining a one-to-many relationship can produce duplicate parent rows in the result set, the ORM cannot reliably calculate the correct row offset at the database level without risking data loss or incorrect page sizes.
In such scenarios, Hibernate may bypass the database-level pagination and load the entire result set into memory to perform the filtering manually. This behavior creates a significant risk of OutOfMemoryError when dealing with large datasets.
Technical Uncertainties
- Under what specific version conditions does Hibernate prioritize memory-side pagination over throwing an exception when a fetch join is detected?
- What is the precise threshold or configuration that determines when the persistence context triggers a warning regarding in-memory pagination?