-->
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.  [ 6 posts ] 
Author Message
 Post subject: Any way to initialize collections in bulk?
PostPosted: Tue Nov 11, 2003 10:57 am 
Newbie

Joined: Tue Nov 11, 2003 10:42 am
Posts: 9
I'm looking for a way or for suggestions on how to initialize lazy loaded collections in bulk.

For example, let's say I have a class like the following:

Code:
class Person
{
  Set childSet;
  Set friendSet;
  Set parentSet;
   ...
}


I have already loaded a list of Person objects via a query. Now that I have the Person objects, is there a way to initialize a collection of Person in bulk (e.g., I pass in a list of Person objects and say that I want the friendSet collection initialized - hibernate performs a single query with an outer join and populates all of the Person objects)? I see that I can iterate thru each one and call Hibernate.initialize but that will make a db call each time.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 11, 2003 11:12 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
In Hibernate 2.1 you can set the batch-size attribute of <set>. Your Java code will look identical.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 11, 2003 12:11 pm 
Newbie

Joined: Tue Nov 11, 2003 10:42 am
Posts: 9
Thanks. That seems to be working but it is behaving oddly regarding the batching.

Is the batch-size setting stable in 2.1b6?

If I set the batch-size to 100, it loads in batches of 10. If I set the batch-size to 50 it loads the first two batches in batches of 7 and the rest one at a time.

My set mappings look like:
Code:
<set
       name="contentInfoEntitySet"
       table="UOI_OBJECTS"
       lazy="true"
       outer-join="true"
       cascade="none"
       batch-size="50"
       >
       <key column="UOI_ID"/>
       <many-to-many column="OBJECT_ID" class="com.acme.server.content.entity.ContentInfoEntity"/>
     </set>
     <set
       name="termEntitySet"
       table="NODES_FOR_UOIS"
       lazy="true"
       outer-join="true"
       cascade="none"
       batch-size="50"
       >
       <key column="UOI_ID"/>
       <many-to-many column="NODE_ID" class="com.acme.server.vocabulary.entity.TermEntity"/>
     </set>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 11, 2003 12:17 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
This is correct (and good) behavior.

Suppose I had 66 instances to load, and batch-size=50:

Batch 1: size 50
Batch 2: size 7
Batch 3: size 7
Batch 4: size 1
Batch 5: size 1


7 = sqrt(50), by the way.

This solution means I can pregenerate the SQL up front, without needed to generate 50 different statements - and it also might help to achieve efficient prepared statement caching.

P.S. I'm not convinced that 50 is a sensible batch size. I would think that 16 or 25 are at least large enough.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 11:08 am 
Newbie

Joined: Wed Feb 11, 2004 10:12 am
Posts: 2
Hi Gavin,

gavin wrote:
This is correct (and good) behavior.

Suppose I had 66 instances to load, and batch-size=50:

Batch 1: size 50
Batch 2: size 7
Batch 3: size 7
Batch 4: size 1
Batch 5: size 1



I looked at Hibernate 2.1.1 and 2.1.2 - it seems to me that it behaves slightly different compared to what you've said:

Setting Batchsize to 50 leads to the following batch size pattern:
Batch 1: size 7!!
Batch2: size 7
.....
So that means to be able to fetch 50 I must set batch-size to 50^2
But unfortunatly hibernate decides to fall back to default batch-size = 1, when batch-size is set too large (I tried 121 - it works so I am able to batch fetch 11 instances, but 225 fails....

It's areal problem for me because we have some huge object graphs to load! Shall I submit it to JIRA as bug?

Cheers,

Manuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 1:34 pm 
Newbie

Joined: Wed Feb 11, 2004 10:12 am
Posts: 2
manuelf wrote:
Shall I submit it to JIRA as bug?


Ok ok - I withdraw that :D

I took a look in the code - batch-size is a difficult area - it's very sensitiv - setting it too high can have a very counterproductive outcome...

I will look deeper into it - maybe that can solve my problems...

Cheers,

Manuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.