-->
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.  [ 3 posts ] 
Author Message
 Post subject: Quering a tree of objects
PostPosted: Wed Feb 17, 2010 8:00 pm 
Newbie

Joined: Wed Feb 03, 2010 9:17 am
Posts: 5
Hello there! We have the following relationship:

Code:
public class Category{

@OneToMany(mappedBy="parent")
private Set<Category> children;

@ManyToOne
private Category parent;

@OneToMany
private Set<Media> medias;



Is it possible to use HQL to query for this:

for each Category, fetch its children
for each children fetch its medias
return the joining of each category's child medias into one big list

I know I could do this as the pseudo code above, using a for loop, and while the session was opened I would query for it. But it seems to be such an impact on the DB. I wonder if there's a better way to do this

Regards


Top
 Profile  
 
 Post subject: Re: Quering a tree of objects
PostPosted: Thu Feb 18, 2010 9:17 am 
Beginner
Beginner

Joined: Wed Nov 21, 2007 8:02 am
Posts: 48
Query query = session.createQuery("select category.id,medias from Category category left join category.children child left join child.medias medias");
List x = query.list();

each element in this collection will be an array of length two. In that array, first element will have the category id and second element will have the all the medias of it's child catergoies.


or If you run the following it will give you list of medias(there won't be an array)

Query query = session.createQuery("select medias from Category category left join category.children child left join child.medias medias");
List x = query.list(); where x is a list of Media objects


Top
 Profile  
 
 Post subject: Re: Quering a tree of objects
PostPosted: Thu Feb 18, 2010 12:23 pm 
Newbie

Joined: Thu Feb 18, 2010 11:54 am
Posts: 1
With "select category.id,medias from Category category left join category.children child left join child.medias medias" I get a list of arrays which second element is a unique media. Categories having many medias are split among as many lines as their medias count.

Isn't there some way to have the medias grouped by category with a unique HQL query?


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