Skip to content

Deduplicate fetch join results at QueryDSL level#1696

Open
zio0911 wants to merge 2 commits intoOpenFeign:masterfrom
zio0911:fix/fetch-deduplication-1596
Open

Deduplicate fetch join results at QueryDSL level#1696
zio0911 wants to merge 2 commits intoOpenFeign:masterfrom
zio0911:fix/fetch-deduplication-1596

Conversation

@zio0911
Copy link
Copy Markdown

@zio0911 zio0911 commented Apr 16, 2026

Summary

  • Deduplicate results in fetch(), fetchOne(), and fetchResults() when fetchJoin() is used
  • Applied in both AbstractJPAQuery (JPA) and AbstractHibernateQuery (Hibernate)
  • Works across all JPA providers — no Hibernate-specific API dependency

Closes #1596

Problem

Since Hibernate 6, automatic result deduplication for fetch joins was removed, and Hibernate 7.3 fully removed the feature. This causes:

  • fetch() returning duplicate parent entities from JOIN results
  • fetchOne() throwing NonUniqueResultException when only one entity was expected

Solution

QueryDSL already knows when fetchJoin() is called (via JoinFlag.FETCH in query metadata). When detected, results are deduplicated using LinkedHashSet (preserves order) before returning.

This is applied at the QueryDSL level rather than using Hibernate's ResultListTransformer because:

  • ResultListTransformer.uniqueResultTransformer() is Hibernate 7.3+ only
  • A QueryDSL-level fix works across all JPA providers
  • Normal queries without fetchJoin are unaffected

Test plan

  • Existing tests pass (./mvnw -Pno-databases verify)
  • No behavioral change for queries without fetchJoin
  • Code formatted (pre-commit hook)

이승영 and others added 2 commits April 16, 2026 13:10
Since Hibernate 6 removed automatic result deduplication for fetch
joins, and Hibernate 7.3 fully removed the feature, queries with
fetchJoin() can return duplicate parent entities from JOIN results.

Add deduplication via LinkedHashSet in fetch(), fetchOne(), and
fetchResults() when fetchJoin is detected, applied at the QueryDSL
level so it works across all JPA providers.

Closes OpenFeign#1596
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JPA #fetchOne returns invalid results

1 participant