-->
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.  [ 2 posts ] 
Author Message
 Post subject: Querying on existence in a set
PostPosted: Tue Feb 21, 2006 11:53 pm 
Newbie

Joined: Mon Sep 05, 2005 10:52 pm
Posts: 6
Hi all,

I've read various old threads about how to do this in Hibernate, but I'm just making sure.

I have a Group objects, which contains a member Set of User objects, named memberUsers. I want to build a Criteria-based query that returns all Group objects whose memberUsers Set contains a given User.

By what I've been reading, my query function should be something like this:

List<Group> getGroupsWithUser(User user)
{
Criteria crit = session.createCriteria(Group.class);
Criteria userCrit = crit.createCriteria("memberUsers");
userCrit.add(Restrictions.eq("this", user));
return crit.list();
}

Can someone confirm, or modify, what is needed to get this to work? Other threads seem to base the criteria on being given some property of a User object in memberUsers, *not* what to do if given a User entity itself. It was buried somewhere that "this" should be used as the Restriction.

Thanks for any confirmation!

--Scott


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 22, 2006 7:37 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Looks good. You'll probably want to put a ResultTransformer on the criteria so that it doesn't return one Group per User. Criteria.DISTINCT_ROOT_ENTITY is good for that.


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