Pagination behavior change when upgrading Hibernate from 5.6 to 6.0 with fetch joins and ORDER BY
29K reputation · 03 Dec 2023, 21:33 UTC
Goal: obtain predictable pagination results for a JPQL/HQL query that uses fetch joins on a OneToMany association and includes an ORDER BY clause when upgrading from Hibernate 5.6 to 6.0 with the MySQL dialect.
In Hibernate 5.6 the limit/offset is applied before distinct root‑entity filtering, which can cause duplicate root rows in the result set. Hibernate 6.0 changes the pagination strategy to use window functions (ROW_NUMBER) and attempts to apply distinct filtering after limiting, but the behavior is not guaranteed for all queries and depends on the hibernate.query.fail_on_pagination_over_collection_fetch setting.
Uncertainty remains about whether Hibernate should automatically add DISTINCT to the SELECT clause when fetch joins are combined with pagination to eliminate duplicates without requiring manual configuration.
What is the recommended approach to guarantee duplicate‑free pagination across the 5.6→6.0 boundary? Should applications rely on the new window‑function strategy and enable the fail‑on‑pagination flag, or is it safer to manually add DISTINCT to the query? How does the choice affect performance and compatibility with other dialects?
1 answer
0 question comments
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.