Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate execute second select for each entity reference

 
avihai marchiano
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • 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
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Justin Chi
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • 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.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
YEP -the query is like you wrote it.
How ever this is not change the question.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic