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.  [ 10 posts ] 
Author Message
 Post subject: Loading child elements of several parents simultaneously
PostPosted: Fri Jan 30, 2004 12:02 am 
Beginner
Beginner

Joined: Thu Nov 20, 2003 8:13 pm
Posts: 31
Location: Newport Beach, CA
Suppose class A has a sole member, a Collection of class B, that must be lazy loaded.

I query for a list of instances of A. Two instances are returned. Assume I cannot left join fetch the children.

Is it possible to initialize all children of both instances of A in a single SQL statement?

My impression is that batch-size only works on children of a single parent. Is this correct?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 30, 2004 6:50 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Why dont you just use join fetch?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 30, 2004 8:21 am 
Expert
Expert

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
Hi

Just to clarify - I think that you (Justin) are confusing the initialisation of the parent and the children.

The 'join fetch' phrase allows you to eagerly initialise a 'child' collection in a join. It doesn't matter how many parent instances there are in the resulting cartesian product - all the elements of the collection that you specify to join fetch will be eagerly initialised. These elements may be associated with different parents, but their collection role is the same (see below)

Now the restriction (from the docs)

Quote:
Note that, in the current implementation, only one collection role may be fetched in a query.


Note the use of the words 'collection role' - What the guys are saying here is that the eagerly fetched collections may manifest itself in more than a single collection (e.g. Parent instance A has a collection containing elements of class X, and Parent instance B has a collection containing elements of class X) but must have the same 'role' (in this case X). So you can't do:
Code:
from eg.Cat as cat
   left join fetch cat.friends
   left join fetch cat.kittens

..assuming cat 1-----* kittens and cat 1------* friends

Note however that you can do (from the docs)
Code:
from eg.Cat as cat
    inner join fetch cat.mate
    left join fetch cat.kittens

.. as the mate association is not a collection.

Lazy initialisation of Parents happens via proxying - which is another kettle of fish...


Top
 Profile  
 
 Post subject: More than one child collection
PostPosted: Fri Jan 30, 2004 4:31 pm 
Beginner
Beginner

Joined: Thu Nov 20, 2003 8:13 pm
Posts: 31
Location: Newport Beach, CA
Yep, I know how left join fetch works, and I cannot use it because it is already being used for another collection. I simplified the problem and specified these restrictions because of this.
So, the question still stands. Is there any way to emulate left join fetch behavior with a single SQL select after the parent objects have already been retrieved?
Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 30, 2004 4:33 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
yes, you can try using batch fetching.


Top
 Profile  
 
 Post subject: batch-sizing
PostPosted: Fri Jan 30, 2004 6:53 pm 
Beginner
Beginner

Joined: Thu Nov 20, 2003 8:13 pm
Posts: 31
Location: Newport Beach, CA
Thanks,
Any way to force batch fetches to not use the SQRT function and just fetch up to batch-size? For example, given 12 children to fetch, they come in at 5, 5, 1, 1 with batch-size 25. This is four SQL statements rather than one. Is there any way to force it to one?
I know you've mentioned in a previous post that there is a reason for the SQRT functionality. Can you elaborate?
Thanks again.


Top
 Profile  
 
 Post subject: Answering my own question
PostPosted: Wed Feb 04, 2004 2:20 pm 
Beginner
Beginner

Joined: Thu Nov 20, 2003 8:13 pm
Posts: 31
Location: Newport Beach, CA
The easy way I have found is to only retrieve child instance ids in the first query, and then use the IN operator in the second query along with the list of child ids, e.g.:

'IN (?, ?, ?, ?, ?, ?)'

to pull in the child-objects. Hand-assembly is required of the resulting associations/collections (I store child objects in a HashMap by id) but it allows for a single SQL select which was the desired effect.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2004 2:22 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
The reason for the sqrt stuff is better optimized sql generation and better usage of the prepared statement cache. You are most likely better of sticking to it than rolling your own.


Top
 Profile  
 
 Post subject: Explanation of sqrt
PostPosted: Thu Feb 05, 2004 8:42 pm 
Beginner
Beginner

Joined: Thu Nov 20, 2003 8:13 pm
Posts: 31
Location: Newport Beach, CA
In my case, it is definitely not advantageous to use batch-size versus IN (...). I simply cannot have N SQL statements running in my query... as the database is 1000 miles away, and the round-trip time for a database hit is not a minor concern. It is much more optimal in my situation to have 1 SQL statement, regardless of any statement caching.
Can you elaborate on how the SQL generation is better optimized in the batch-size scenario?
Thx


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2004 4:22 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Just switch on batch fetching and look at the generated SQL.


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