-->
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.  [ 5 posts ] 
Author Message
 Post subject: One-To-Many collection searching
PostPosted: Tue Dec 30, 2008 10:13 pm 
Newbie

Joined: Fri Nov 21, 2008 3:35 pm
Posts: 12
I need to find a list of users who have a collection with an object with a property that has a value. Wow, that's a mouthful. Here's a simplification of my domain model:

User -> interests (List) -> interests -> relationId

I need to find a User who has an interest that has a relationId matching a value I'm inputting. Does anyone know how to do this?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 31, 2008 12:39 am 
Regular
Regular

Joined: Wed Oct 15, 2008 6:59 am
Posts: 103
Location: Chennai
get all users objects by HQL

from Users;

then iterate each users object and get interest list, after that get each interest and check out with ur realtionid , if it is map copy that object to new users object.

Iterator rs=session.createQuery("from Users");
while(rs.hasNext())
{
Users u =(Users) rs.next();
List<Interest> inter=u.getInterest();
for(int i=0;i<inter.size();i++)
{
Interest it=inter.get(i);
if(it.getRelationId()==myRelationId) //ur Id comparision
sys.out.print(u.getName());
}
}


like that u will get all users with ur relationId...

_________________
If u feel it will help you, don't forget to rate me....


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 31, 2008 11:40 am 
Newbie

Joined: Tue Dec 30, 2008 6:20 pm
Posts: 4
Location: Bulgaria
You can try the following HQL:
Code:
String hql = "select interest
                       from User user
                       join user.interests interest
                       where interest.relationId = :relationId";
List<Interest> inter = session.createQuery(hql).list();

_________________
Nikolai Gagov


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 31, 2008 1:56 pm 
Newbie

Joined: Fri Nov 21, 2008 3:35 pm
Posts: 12
Thanks for your replies. Nikolai, is there a way to reproduce the equivalent of your HQL in the Criteria API?

I want to do most of the work in my select statements rather than handfilter the results on the Java side. I got it working by iterating through the result lists as Madan suggested, but I'd like to not have to iterate over lists and let Hibernate just filter the results from the database.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 02, 2009 8:57 am 
Regular
Regular

Joined: Wed Oct 15, 2008 6:59 am
Posts: 103
Location: Chennai
List Users= sess.createCriteria(Users.class)
.createAlias("interests", "inter")
.add( Restrictions.eqProperty("inter.relationId ",relationid) )
.list();

_________________
If u feel it will help you, don't forget to rate me....


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.