-->
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.  [ 4 posts ] 
Author Message
 Post subject: Fetch Collection in HQL
PostPosted: Fri Jan 08, 2010 8:51 am 
Newbie

Joined: Tue Dec 01, 2009 9:34 am
Posts: 3
Hi all,

I have a doubt about how can I fetch a collection in a HQL command. If someone could help me, i'm thank a lot =D

I hava this Domain model:

Code:
public class Category {
   public int Id { get; set; }
   public string Name { get; set; }
   public IList<SubCategory> SubCategories { get; set; }
}

public class SubCategory {
   public int Id { get; set; }
   public string Name { get; set; }
   public Category Category { get; set; }
}




I would Like to know, How can I do a command that return a IList of Category and for each object in this list, fetch the IList<SubCategory> with a sub-query command ? I have been test this command:

string hql = "from Category c inner join fetch c.SubCategories order by a.Name";

This command works, but this create a Category object for each subcategory... and repeat categories in my list (menu of my website)

Well, if someone can help... Thanks a lot

PS: Sorry for my English

cheers


Top
 Profile  
 
 Post subject: Re: Fetch Collection in HQL
PostPosted: Fri Jan 08, 2010 9:43 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
The created objects are not really duplicates. The returned list just contains the same object multiple times. This happens because you make a join fetch. You can remove the duplicate entries with this:

IList<Category> result = query.SetResultTransformer( CriteriaUtil.DistinctRootEntity ).List<Category>();


Have a look here if you look for an explanation of this behavior:

http://www.hibernate.org/117.html#A12

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: Fetch Collection in HQL
PostPosted: Sat Jan 09, 2010 2:16 am 
Newbie

Joined: Sat Jan 09, 2010 2:08 am
Posts: 1
Location: usa
I am very simple person so I am not understand this site so please I request to you that you help me..........

_________________
Force Factor Supplements


Top
 Profile  
 
 Post subject: Re: Fetch Collection in HQL
PostPosted: Thu Jan 14, 2010 10:00 am 
Newbie

Joined: Tue Dec 01, 2009 9:34 am
Posts: 3
Now, it's working very fine!!!

Thanks guys!

Cheers


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