Inconsistent page size when using Hibernate fetch join with setMaxResults
27K reputation · 19 Jul 2025, 14:24 UTC
When a Hibernate query uses a fetch join (e.g., left join fetch) together with pagination via setFirstResult and setMaxResults, the application expects each page to contain a exact number of distinct root entities.
However, the LIMIT/OFFSET clause is applied to the joined result set before Hibernate removes duplicate rows, so the number of distinct entities returned can be larger or smaller than the requested page size. This raises the question of whether pagination limits should be applied to the raw joined rows or to the deduplicated entity set, and what configuration or behavior change would achieve consistent paging without losing the benefits of fetch joins.
- Should Hibernate apply pagination before or after deduplication of root entities?
- Is there a setting that enforces post‑deduplication limits while preserving fetch join efficiency?
- What impact would changing the current limit‑application point have on existing applications?