Diamond shaped OneToMany + FetchType.EAGER + FetchMode.JOIN causes N+1 selects
Description
Attachments
Activity
Pit Humke July 19, 2018 at 9:06 AM
Update: The issue also exists when using MultiLoadAccess to load multiple "TOP" instances at once. Apparently, when querying the objects this way, Hibernate uses a redundant implementation of the metamodel walking (and SQL creation) - which seems unnecessary to me. Anyways, the "fix" is the same. As a workaround, I just commented out the cycle detection and that seems to make it work (here and also here).
Pit Humke July 10, 2018 at 7:32 PMEdited
Update: Making this function statically return `false` and commenting out this block "fixes" the problem. Of course this is not a solution, but it's a start
Pit Humke July 10, 2018 at 6:47 PMEdited
Update: While debugging this issue, I found that the `MetamodelGraphWalker` might be to blame here. It looks to me like the detection of circular associations does not work correctly, because the `MetamodelGraphWalker` thinks that it detected a cycle in the metamodel graph (see above) when it is actually just a directed acyclic graph. See here and here.
Also, it looks like Gail Badner ("gbadner") already thought of this problem when he made this commit back in 2013.
Problem summary:
When mapping "diamond shaped" OneToMany relationships using FetchType.EAGER and FetchMode.JOIN, Hibernate generates N+1 select statements. Here is what I mean with "diamond shape":
Expected behavior:
A single select statement with two joins on the "BOTTOM" table.
Actual behavior:
Detailed explanation and code sample:
https://stackoverflow.com/q/51199440/3216347
Test Case:
https://github.com/hibernate/hibernate-orm/pull/2402