-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: How database calls are made to load a set (one-to-many)
PostPosted: Tue Jan 06, 2004 6:47 pm 
Beginner
Beginner

Joined: Tue Oct 07, 2003 12:11 pm
Posts: 32
Location: US
here is an interesting situation

Code:
<class name="abc.def.Class1" ........>
<id column="....></id>
<property column="......"/>
<property column="......"/>
    <set name="myName" lazy="false" inverse="true">
        <key column = "myname_uid"/>
        <one-to-many class="abc.def.Class2"/>
    </set>
</class>


I execute a Query and got a List of abc.def.Class1
and each abc.def.Class1 had one or more abc.def.Class2

Let us consider the case when abc.def.Class1 is loaded.

I ran my app in debug mode, i saw that a prepared statement was created to load abc.def.Class1

I have to load 10 abc.def.Class2 objects in a set corresponding to the load of one abc.def.Class1 (as per the one-to-many relation)

When abc.def.Class1 loads, I saw totally 11 times prepared statements being created. I think 11 db calls were made.

When we do this in a normal DAO, we would have at the max of 2 db calls.

Can any one let me know how Hibernate will lode data / make db calls?

I am sure that Hibernate is faster in DB access. so why did it try to make 11 prepared statements? Could any one please guide me to know how this works? Any help would be much appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 06, 2004 8:12 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
I am pretty sure you used Query.iterate. If you just do a load on Class1, hibernate will produce 1 select for Class1, and 1 select for Class2


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 07, 2004 3:41 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
iterate will face the n+1 issue since it use the cache
find or load will do it in 1 request (not using cache)

Read the javadoc on that subject

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.