Skip to content

fix(GH-216): One-to-many queries joins the same tables twice #217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 1, 2019

Conversation

igdianov
Copy link
Collaborator

@igdianov igdianov commented Dec 1, 2019

This PR removes redundant entity graph hint causing duplicate fetch joins on the same tables.

Fixes #216

Given GraphQL query:

query { 
  Humans(where:{id:{EQ: "1000"}}) {
    select {
      name, homePlanet, friends { name } 
    }
  }
}

Then generated JPQL query

select distinct human 
from Human as human left join fetch human.friends as generatedAlias0 
where human.id=:param0 order by human.id asc

is transformed by Hibernate into SQL query without duplicate joins:

    select
        human0_.id as id2_0_0_,
        character2_.id as id2_0_1_,
        human0_.name as name3_0_0_,
        human0_.favorite_droid_id as favorite6_0_0_,
        human0_.gender_code_id as gender_c7_0_0_,
        human0_.homePlanet as homePlan4_0_0_,
        character2_.name as name3_0_1_,
        character2_.primary_function as primary_5_0_1_,
        character2_.favorite_droid_id as favorite6_0_1_,
        character2_.gender_code_id as gender_c7_0_1_,
        character2_.homePlanet as homePlan4_0_1_,
        character2_.DTYPE as DTYPE1_0_1_,
        friends1_.source_id as source_i1_2_0__,
        friends1_.friend_id as friend_i2_2_0__ 
    from
        Character human0_ 
    left outer join
        character_friends friends1_ 
            on human0_.id=friends1_.source_id 
    left outer join
        Character character2_ 
            on friends1_.friend_id=character2_.id 
    where
        human0_.DTYPE='Human' 
        and human0_.id=? 
    order by
        human0_.id asc,
        character2_.name asc

@igdianov igdianov self-assigned this Dec 1, 2019
@igdianov igdianov added the bug label Dec 1, 2019
@igdianov
Copy link
Collaborator Author

igdianov commented Dec 1, 2019

@molexx Check this out. Apparently having entity fetch graph hint makes Hibernate to add duplicate fetch joins to the query. The library already builds query with fetch joins from selection, so I think it is not needed any more.

I think it may actually help resolve some unexplained issues with embedded entity sort order, and many-to-many search criterias. I will investigate this further.

@codecov
Copy link

codecov bot commented Dec 1, 2019

Codecov Report

Merging #217 into master will decrease coverage by 0.4%.
The diff coverage is 100%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master     #217      +/-   ##
============================================
- Coverage     74.05%   73.65%   -0.41%     
+ Complexity      882      862      -20     
============================================
  Files            50       50              
  Lines          3754     3727      -27     
  Branches        628      624       -4     
============================================
- Hits           2780     2745      -35     
- Misses          696      707      +11     
+ Partials        278      275       -3
Impacted Files Coverage Δ Complexity Δ
...a/query/schema/impl/QraphQLJpaBaseDataFetcher.java 73.53% <ø> (-2.29%) 150 <0> (-20)
.../query/schema/impl/GraphQLJpaQueryDataFetcher.java 92.55% <ø> (-0.16%) 26 <0> (ø)
...query/schema/impl/GraphQLJpaSimpleDataFetcher.java 90% <100%> (-1.31%) 7 <0> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0151eec...0627722. Read the comment docs.

@igdianov igdianov merged commit 6e204fc into master Dec 1, 2019
@igdianov igdianov deleted the fix-GH-216-query-joins-twice branch December 1, 2019 08:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

One-to-many queries join the same tables twice
1 participant