Does Hibernate automatically apply distinct to limit results when using fetch joins?
0 reputation · 27 Jan 2024, 18:56 UTC
The goal is to retrieve a limited set of distinct parent entities (e.g., Authors) while eagerly fetching their child collections (e.g., Books) using a fetch join, without returning partial collections caused by the limit cutting off duplicate rows.
The uncertainty lies in whether Hibernate automatically adds DISTINCT to collapse duplicates before applying setFirstResult/setMaxResults, or whether the developer must explicitly enable the 'hibernate.query.passDistinctThrough' hint or use ResultTransformer.DISTINCT_ROOT_ENTITY, especially considering version differences between 5.x and 6.x.
Does Hibernate 6.x enable distinct by default for fetch joins, or does it still depend on the dialect and hint?
If the default behavior does not apply distinct, which combination of hint or ResultTransformer guarantees that the limit operates on distinct entity rows without requiring a subquery?
How does the behavior differ when the same pagination is expressed in HQL versus the Criteria API?