-->
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: Regarding retrieval of multiple objects
PostPosted: Tue Jun 20, 2006 7:07 am 
Newbie

Joined: Tue Jun 20, 2006 4:58 am
Posts: 2
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Consider the following association

A <----> B <----->*C

A has a one to one relation with B
B has a one to many relation with C

I have the following query

select a, b, c from A as a inner join a.b as B left outer join b.c as C where a.attrib in ('zzz','www','yyy')

This query on execution, returns a list of Object[], with each element in the array having an instance each of A,B,C

for example, the result will be as follows

[a1, b1, c11]
[a1, b1, c12]
[a1, b1, c13]

[a2, b2, c21]
[a2, b2, c22]
[a2, b2, c23]

[a3, b3, c31]
[a3, b3, c32]
[a3, b3, c33]

Is there a way to retrieve the result in the following format (using a single EJB QL / HQL query) ?

[a1,b1, set(c1)]
[a2,b2, set(c2)]
[a3,b3, set(c3)]

where, set(c1) will contain objects c11,c12,c13 which are associated to b1 and similarly set(c2) will contain c21,c22,c23 and set(c3) will contain c31,c32,c33

The idea is basically to retrieve a group of associated objects using a single EJB QL /HQL query. Any help or pointers regarding this would help.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 20, 2006 11:29 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
No, but don't you already have that set in b? If you use "select a, b from A ...", then get the set of Cs as b.getC(), isn't that good enough?

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 20, 2006 11:38 pm 
Beginner
Beginner

Joined: Fri Jun 02, 2006 1:14 am
Posts: 30
tenwit wrote:
No, but don't you already have that set in b? If you use "select a, b from A ...", then get the set of Cs as b.getC(), isn't that good enough?

Wouldn't that create an "N+1 selects" problem ?

(Sorry to butt in, but I have a similar pattern in my own code, so I'm curious)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 20, 2006 11:43 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Not in this case, because of the join in the query. C isn't in the select, but it is in the query.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 1:47 am 
Newbie

Joined: Tue Jun 20, 2006 4:58 am
Posts: 2
The problem is that I'm trying to store the result sets as a serialized object - i mean storing each object array as a serialized object which will be used later on - like [a1,b1,set(c1)] as a serailized object. So having the results this way would not work out in my case.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 5:34 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Then you'll have to write your serialization code to take account of the fact that the Set<C> is not in ((Object[]) result)[2]), but in ((B)((Object[]) result)[1]).getC().

_________________
Code tags are your friend. Know them and use them.


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.