http://davybrion.com/blog/2008/10/querying-with-nhibernate/
that has 2 examples of using subqueries On Fri, Mar 13, 2009 at 10:56 AM, James Crowley <[email protected]>wrote: > hey everyone, > > I've been trying to track down some detailed examples of subqueries using > criteria (for either hibernate or nhibernate) but have failed miserably. If > there's any good place to look, please do let me know. > > Failing that... my query looks like this: > > ICriteria c = _queryService.Session.CreateCriteria(typeof > (Content),"content") > .CreateAlias("WorkflowStage", "ws") > .CreateAlias("Owner", "u") > .CreateAlias("ws.TransitionsFromHere", "trn") > .Add(Restrictions.Eq("ws.Active", false)) > .Add(Restrictions.Eq("u.Id", userContext.Identity.Id)) > .Add(Restrictions.In("trn.Role", > userContext.Identity.Roles.ToArray())); > ... > c.SetProjection(p); > c.SetResultTransformer(Transformers.AliasToBean(typeof(ContentView))); > > at the moment. The problem is that really I need an Exists() type query for > the last clause, because I only want a each Content element that has at > least one "trn.Role" that matches. > > I've tried doing this: > > DetachedCriteria rolesCriteria = DetachedCriteria.For(typeof > (WorkflowTransition),"trn") > .Add(Restrictions.In("Role", userContext.Identity.Roles.ToArray())) > .SetProjection(Projections.Property("Id")); > > ICriteria c = _queryService.Session.CreateCriteria(typeof > (Content),"content") > .CreateAlias("WorkflowStage", "ws") > .CreateAlias("Owner", "u") > .Add(Restrictions.Eq("ws.Active", false)) > .Add(Restrictions.Eq("u.Id", userContext.Identity.Id)) > .Add(Subqueries.Exists(rolesCriteria)); > > but I'm unsure how to "attach" the detached criteria so it's linked to the > content element. I've seen some examples using Restrictions.EqProperty... > but I don't have a visible "Id" column on WorkflowTransition - just a direct > reference back to it's WorkflowStage parent. > > Any advice or pointers would be much appreciated! > > Thanks > > James > > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "nhusers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/nhusers?hl=en -~----------~----~----~----~------~----~------~--~---
