-->
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: How to query with HQL from Many-to-Many?
PostPosted: Fri Sep 16, 2005 3:23 am 
Newbie

Joined: Fri Sep 16, 2005 3:08 am
Posts: 15
Hi, All

I have a class User which has a set of Role, User and Role are hibernate mapped POJO. User and Role are many-to-many mapped.
Now I want to query all users which has multiple specified roles (The number of roles to match is dynamicly specified by user through the page).
I don't know how to construct the HQL for this query. I hope there's some thing like:

from User as user where user.roles intersect (select role from Role where role.id in (:roleFilter))

here the roleFilter may be "10,11,14,39"

Obviously the above hql will not work.

It's great if it can work like:

from User as user where user.roles contains some (select role from Role where role.id in (:roleFilter))

or

from User as user where user.roles in (select role from Role where role.id in (:roleFilter))

Can anybody give me a correct HQL solution to this problem?

Thank you so much,
Yours,


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 16, 2005 8:46 am 
Contributor
Contributor

Joined: Thu Nov 06, 2003 9:49 pm
Posts: 104
Location: New York, NY
Use IN and a subquery.

See:
http://forum.hibernate.org/viewtopic.php?t=947645

HTH


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 16, 2005 12:24 pm 
Newbie

Joined: Fri Sep 16, 2005 3:08 am
Posts: 15
Dear joshua,

Thank you so much. It works quite well. The point is that I can use User.roles.id. I only thought that I need to extract each role with such as elements(User.roles).id.

Yous,
wang


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.