• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

More than 30 joins in a query because polymorphism

 
avihai marchiano
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following class hierarchy:

Class AAA contain reference to BBB (name - bbb)

Class AAA2 inherit from class AAA (so also have this reference)



Class BBB2,BBB3,BBB4,BBB5,BBB6 inherit from BBB.



Actually class AAA2 can only has reference to BBB6.



When I call to find by id on AAA2 instance the query that generated by hibernate has more than 5 left outer join because that AAA2 has reference (due to inheritance from AAA ) to BBB and BBB has 5 classes that extend it.



When loading AAA2 there is no need to execute left outer join on all BBB descendents, because he can have reference only to BBB6.



Can you advice on good practice for this. I get queries in more than 30 joins.



Thank you
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is all in the mapping. How is the inheritance mapped? You can set them to be lazy loaded. When lazy loaded there shouldn't be any joins, except in some inheritance stuff. But AAA to any BBB should not have joins if mapped lazyily.

Mark
 
avihai marchiano
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your answer.

I cant have those fields as lazy.
when i load AAA2 i want also to load the BBB reference.

The problem is that hibernate will do left outer join on all BBB subtype, despite that AAA2 can hold any reference except BBB6.
Hibernate dosnt know this , so it does left outer join on all subtype.
i wish i have a way to let hibernate know that BBB6 is the only referece that is relevant for AAA2.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic