aspose file tools*
The moose likes Object Relational Mapping and the fly likes More than 30 joins in a query because polymorphism Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "More than 30 joins in a query because polymorphism" Watch "More than 30 joins in a query because polymorphism" New topic
Author

More than 30 joins in a query because polymorphism

avihai marchiano
Ranch Hand

Joined: Jan 10, 2007
Posts: 342
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

Joined: Feb 05, 2001
Posts: 17257
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
avihai marchiano
Ranch Hand

Joined: Jan 10, 2007
Posts: 342
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: More than 30 joins in a query because polymorphism