Deduplicate fetch join results at QueryDSL level#1696
Open
zio0911 wants to merge 2 commits intoOpenFeign:masterfrom
Open
Deduplicate fetch join results at QueryDSL level#1696zio0911 wants to merge 2 commits intoOpenFeign:masterfrom
zio0911 wants to merge 2 commits intoOpenFeign:masterfrom
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
fetch(),fetchOne(), andfetchResults()whenfetchJoin()is usedAbstractJPAQuery(JPA) andAbstractHibernateQuery(Hibernate)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 resultsfetchOne()throwingNonUniqueResultExceptionwhen only one entity was expectedSolution
QueryDSL already knows when
fetchJoin()is called (viaJoinFlag.FETCHin query metadata). When detected, results are deduplicated usingLinkedHashSet(preserves order) before returning.This is applied at the QueryDSL level rather than using Hibernate's
ResultListTransformerbecause:ResultListTransformer.uniqueResultTransformer()is Hibernate 7.3+ onlyTest plan
./mvnw -Pno-databases verify)