-->
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.  [ 13 posts ] 
Author Message
 Post subject: Join fetch of multiple collections in HQL?
PostPosted: Tue Mar 08, 2005 5:45 pm 
Newbie

Joined: Tue Mar 08, 2005 5:32 pm
Posts: 11
The Hibernate doc make the following statement regarding join fetch in HQL queries:

Note that, in the current implementation, only one collection role may be fetched in a query (everything else would be non-performant).

Is this limitation still in Hibernate 3?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 08, 2005 7:05 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
yes, still valid in H3


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 08, 2005 7:15 pm 
Newbie

Joined: Tue Mar 08, 2005 5:32 pm
Posts: 11
Are there any plans to solve that issue? Would it be difficult to do?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 10, 2005 9:36 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
We probably will do it eventually.
As a workaround use the batch-size attribute.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 10, 2005 3:55 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
or execute two queries, one per collection
(only usefull for two collections from the same root entity B*--A--*C)

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 10, 2005 6:12 pm 
Newbie

Joined: Tue Mar 08, 2005 5:32 pm
Posts: 11
Well, what I'll do in mean time is to iterate through the collections before returning them to the web layer. Needless to say that it will not perform.

If I would have a look at solving this, can you give me a hint of where to look?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 11, 2005 12:19 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Don't iterate. Take anthony's advice as that will be the most performant:

Code:
s.createQuery("from RootEntity r inner join fetch r.firstCollection").list();
List result = s.createQuery("from RootEntity r inner join fetch r.seconfCollection").list();


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 11, 2005 1:14 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Just incase you don't follow. The above solution is preloading the session cache with the first query which is being access to (further) fillout the objects in the second query. I have used similar approaches for lookups etc when appropriate. Kinda neat really.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 11, 2005 8:29 pm 
Newbie

Joined: Tue Mar 08, 2005 5:32 pm
Posts: 11
My situation is that I have A - C1 - C2, i.e. when loading A I want to load the corresponding entries in C1 and C2.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 12, 2005 12:17 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
So are both C1 and C2 collection properties on A?

Thats called "in series", where A has a collection named Bs whose elements are of type B which then has a collection named Cs with elements of type C. Hibernate can handle fetching all this at once.

The other situation is called "in parallel", where A has two collections itself. This is where you'd need to perform the stuff suggested in these responses.

So which situation is it you have?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 12, 2005 10:35 am 
Newbie

Joined: Tue Mar 08, 2005 5:32 pm
Posts: 11
The exact situation is like this:

C1
|
B - A - C - C2
|
C3

B and C are properties of A. C1, C2 and C3 are propterties of C. B, C1 and C2 are collections, C3 is a single relation.

When I load A I want all the others to be loaded as well. So as I understand it, I should do like this:


s.createQuery("from A as a left join fetch a.b as b where a.id = :id").list();
s.createQuery("from A as a left join fetch a.c as c left join fetch c.c1 as c1 where a.id = :id").list();
List result = s.createQuery("from A as a left join fetch a.c as c left join fetch c.c2 as c2 left join fetch c.c3 as c3 where a.id = :id").list();

I fetch c2 and c3 in the same query since c3 is not a collection. Would this work?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 12, 2005 12:23 pm 
Newbie

Joined: Tue Mar 08, 2005 5:32 pm
Posts: 11
I've tried two variants:

session.createQuery("select category from Category as category " +
"left join fetch category.parent as parent " +
"left join fetch category.sub " +
"where category.id = :categoryId")
.setLong("categoryId", id.longValue() ).list();

session.createQuery("select category from Category as category " +
"left join fetch category.content as content " +
"left join fetch content.filesets " +
"left join fetch content.price " +
"where category.id = :categoryId")
.setLong("categoryId", id.longValue()).list();

List result = session.createQuery("select category from Category as category " +
"left join fetch category.content as content " +
"left join fetch content.properties " +
"where category.id = :categoryId")
.setLong("categoryId", id.longValue() );

List result = query.list();
Category c = (Category) result.get(0);

category.parent and content.price are single relations, the rest are collections.

Then I tried a simpler variant where I only tried to fetch category.parent, category.sub and category.content.

session.createQuery("select category from Category as category " +
"left join fetch category.parent as parent " +
"left join fetch category.sub " +
"where category.id = :categoryId")
.setLong("categoryId", id.longValue() ).list();

List result = session.createQuery("select category from Category as category " +
"left join fetch category.content as content " +
"where category.id = :categoryId")
.setLong("categoryId", id.longValue()).list();

The result was the same in both cases, only category.parent and category.sub are fetched, category.content is not.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 12, 2005 2:30 pm 
Newbie

Joined: Tue Mar 08, 2005 5:32 pm
Posts: 11
I see that formatting is not correct, I try again:

C1 --+
C2 - C - A - B
C3 --+

The relation multiciply is like this:

A - B 1-*
A - C *-*
C - C1 *-1
C - C2 1-*
C - C3 1-*

Headace generating...


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