-->
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: Query on two many-to-many relationships
PostPosted: Mon Aug 20, 2007 10:36 am 
Newbie

Joined: Mon Aug 20, 2007 10:29 am
Posts: 2
I am having trouble figuring out how to define a query, if anyone can help it would be much appreciated. My entity model is something like

User
has many UserGroups

Group
has many ToolGroups
has many UserGroups

Tool
has many toolGroups

ToolGroup
has a Tool
has a Group

UserGroup
has a User
has a Group

I need a query that will list all the ToolGroups for a given Tool/User - in SQL it would be:

select tg.*
from tblUserGroup ug

inner join tblToolGroup tg on tg.GroupId=ug.GroupId

where
ug.UserId=@UserId
and tg.ToolId=@ToolId


1) How would this be done in NHibernate?
2) Can it be done in NHibernate using a Criteria Query?

Thank you so much!!!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 20, 2007 6:11 pm 
Newbie

Joined: Mon Aug 20, 2007 10:29 am
Posts: 2
Nevermind! I found the solution...

Code:
return session
     .CreateCriteria(typeof(ToolGroup), "Tools")
     .CreateCriteria("Group", "Group")
     .CreateCriteria("Users", "Users")
     .Add(Expression.Eq("Users.Person", person))
     .Add(Expression.Eq("Tools.Tool", tool))
     .SetResultTransformer(CriteriaUtil.RootEntity)
     .List<ToolGroup>();


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.