Hibernate fetch‑join pagination without implicit DISTINCT
0 reputation · 18 Aug 2024, 22:33 UTC
Goal: configure Hibernate so that a paginated HQL query with a fetch join returns the requested number of distinct root entities without manually adding DISTINCT to the query string.
Constraint: Hibernate 5.x and 6.x do not inject an implicit DISTINCT when setFirstResult/setMaxResults are used together with a fetch join, causing duplicate SQL rows and potentially fewer distinct entities per page.
Uncertainty: whether a global configuration property, query hint, or result transformer can enable this behavior transparently, and what the trade‑offs are for second‑level cache usage and database optimizer choices.
Questions: Is there a Hibernate configuration option to enable implicit DISTINCT for paginated fetch‑join queries? Can a query hint or Criteria API setting achieve the same effect without altering the HQL? What verification steps confirm that the page size matches the distinct root entity count in a production‑like environment?