-->
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: query with collections criteria
PostPosted: Thu Apr 26, 2007 9:32 am 
Newbie

Joined: Tue Mar 27, 2007 4:45 pm
Posts: 9
Hello everybody

I've a many-to-many connection with the objects User and Role. The appropriate part of the xml-configuration file of User is as follows:
Code:
bag name="Roles" table="RoleUsers">
         <key column="UserName" />
         <many-to-many column="RoleID" class="MF.DataLayer.Domain.Role" />
      </bag>


What I want is to create a query that returns the User objects that have a specific role. It should look like following:
Code:
session.createQuery("FROM User u WHERE u.Roles.Name = :roleName")


Unfortunately the documentation lacks a little bit concerning queries with that kind of collections.

Could somebody give me a hint on this?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 26, 2007 4:24 pm 
Newbie

Joined: Fri Apr 20, 2007 12:35 am
Posts: 3
Hi,

One way is:

session.createQuery("Select u FROM User u INNER JOIN u.Roles r WHERE r.Name = :roleName")

Hope this helps!


Shawn


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 01, 2007 8:55 am 
Newbie

Joined: Tue Mar 27, 2007 4:45 pm
Posts: 9
Hello,

no that does not help because Roles is a collection of Role objects. When I execute the query I get
Quote:
expecting 'elements' or 'indices' after Name [FROM User u WHERE u.Roles.Name = :roleName]
[/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 01, 2007 9:00 am 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
The exception message you have indicates that the query you're testing is *not* the same as the query suggested by shware.

He is saying to use

Code:
SELECT u FROM User u INNER JOIN u.Roles r WHERE r.Name = :roleName


instead of the query you've used

Code:
SELECT u FROM User u WHERE u.Roles.Name = :roleName


The important distinction is the join.

Cheers,

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 10, 2007 6:04 pm 
Newbie

Joined: Tue Mar 27, 2007 4:45 pm
Posts: 9
Thanks to both of you. It's working now!


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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.