• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hibernate execute second select for each entity reference

 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,

I have Person->Address->*Contacts (collection of elements)

(in my model i have heirarchy with more levels).

i use the following query:
from Person person left join fetch Address address left join fetch address.contacts

in the SQL that hibernate generate i notice that it excute select for each person references, so if i load 10 persons it will run 10 selects for address + 10 selects for contacts instead of load them in a join.

Any idea how to start debug this?

Thank you
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, if it's giving you the correct results, I wouldn't debug it. Wouldn't debugging something that works break it?

It's all done within a single transaction, so it should be relatively efficient. Is this a significant bottleneck in your application?

-Cameron McKenzie
 
avihai marchiano
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This cause a serioes performance issue.
One query instead on join between all entities loaded execute with select for each root entity reference.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you think there should only be one query ,but hibernate generates 1+n please post your java code and hbm.xml.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, the query isn't correct

from Person person left join fetch Address address left join fetch address.contacts

your "fetch Address address" is not valid eager fetch. You can't use the Address class like that

you probably mean

FROM Person person LEFT JOIN FETCH person.address address LEFT JOIN FETCH address.contacts

I like to capitalize keywords, but you don't have to. The key is it should be

person.address address

NOT

Address address

You got the last fetch correct though
Mark
 
avihai marchiano
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
YEP -the query is like you wrote it.
How ever this is not change the question.
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That query will not cause 10 queries to run unless you mapped Person to be always create proxies.

The query is like I wrote. If you still have a problem, then it must be in your mapping.

Please post your Person mapping and just your Address mapping.

Mark
 
reply
    Bookmark Topic Watch Topic
  • New Topic