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.  [ 8 posts ] 
Author Message
 Post subject: How can I late-fetch child data for a *list* of POJOs?
PostPosted: Fri Jul 22, 2005 8:47 am 
Beginner
Beginner

Joined: Fri Jul 08, 2005 8:09 am
Posts: 28
I have a list of POJOs which have some uninitialized collection properties (one-to-many). Is there a way to fetch this collection property for each of the POJOs in a single query, without one query per POJO in a loop?

pojo1.children
pojo2.children
pojo3.children
pojo4.children
...

Actually the problem goes one step further: The direct collection property is actually initialized but the objects in this collection again have set properties which I need to fetch now, without a query for every one of them... I can only think of doing it manually. Collect the ids of the needed objects in a loop, then execute a query with an in clause. But if I do this I have a separate list of the late-fetched objects and have to somehow distribute/associate them with original list of POJOs.

Hibernate version: 3.0.5
Name and version of the database you are using: Oracle 9


Top
 Profile  
 
 Post subject: Re: How can I late-fetch child data for a *list* of POJOs?
PostPosted: Fri Jul 22, 2005 8:57 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
DC wrote:
I have a list of POJOs which have some uninitialized collection properties (one-to-many). Is there a way to fetch this collection property for each of the POJOs in a single query, without one query per POJO in a loop?

pojo1.children
pojo2.children
pojo3.children
pojo4.children
...

Actually the problem goes one step further: The direct collection property is actually initialized but the objects in this collection again have set properties which I need to fetch now, without a query for every one of them... I can only think of doing it manually. Collect the ids of the needed objects in a loop, then execute a query with an in clause. But if I do this I have a separate list of the late-fetched objects and have to somehow distribute/associate them with original list of POJOs.

Hibernate version: 3.0.5
Name and version of the database you are using: Oracle 9


If your session is still open, just accessing the property will cause hibernate to initialize it. Assuming that your session will be closed prior to accessing these collections, you have use Hibernate.initialize(pojo.children) to force initialization of the collection prior to closing the session.

You should also look at the batch-size property in the reference docs.


Top
 Profile  
 
 Post subject: Re: How can I late-fetch child data for a *list* of POJOs?
PostPosted: Fri Jul 22, 2005 9:25 am 
Beginner
Beginner

Joined: Fri Jul 08, 2005 8:09 am
Posts: 28
pksiv wrote:
If your session is still open, just accessing the property will cause hibernate to initialize it. Assuming that your session will be closed prior to accessing these collections, you have use Hibernate.initialize(pojo.children) to force initialization of the collection prior to closing the session.

You should also look at the batch-size property in the reference docs.


I know that the initialization is occurring transparently. But a loop over the pojos, calling initialize on each one, will result in n database hits, right? That's what I must avoid.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 22, 2005 12:14 pm 
Senior
Senior

Joined: Tue Jun 21, 2005 10:18 am
Posts: 135
Location: South Carolina, USA
as pksiv said, look at the batch-size attribute.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 22, 2005 5:41 pm 
Beginner
Beginner

Joined: Fri Jul 08, 2005 8:09 am
Posts: 28
Both of you are completely right (credits allotted), batch-size did the trick. Thanks a lot.

I could not imagine that this would work because previously I made the experience that pojos that I had loaded via a criteria query were reloaded when I wanted to access them via session.load() later on - although the query did return them already and Hibernate knew about them. From this I concluded that not even criteria queries, based on a single entity, would be cached (without explicit query cache and L2 level cache). But if they are not cached how could "batch-size" know about other pojos which need initialization? This remains in the dark for me... but somehow it works ;-)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 15, 2005 6:38 pm 
Newbie

Joined: Thu Apr 29, 2004 1:09 pm
Posts: 4
Could you please give some details on batch-size? Is it possible to enable this with the EJB3 EntityManager. I'm using jboss-4.0.3RC2.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 16, 2005 5:20 am 
Beginner
Beginner

Joined: Fri Jul 08, 2005 8:09 am
Posts: 28
I am not using the EJB3 EntityManager or JBoss. But there is not much to it, just set the batch-size attribute on the association mapping or the table mapping. The documentation has an example for this (look for batch-size).

Basically it works like a speculative fetch. If you need the child data for one pojo it will load child data for other then known pojos, too. Just in case you might need them later. The value of batch-size determines the number of pojos for which child data records are fetched. HTH.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 19, 2005 4:56 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Code:
@BatchSize(size=4)

_________________
Emmanuel


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