What would be the syntax in an nhibernate (version 3.3) query (criteria?) for the following SQL statement:
SELECT DISTINCT usr.* FROM User usr, User_SecurityGroup grp, SecurityGroup_Permission prm, Permission org
WHERE usr.Id = grp.User_id AND grp.SecurityGroup_id = prm.SecurityGroup_id AND org.Id = prm.Permission_id
AND org.Site_id IN (1,2,3) AND org.PermRead = 1 AND usr.Active = 1 AND org.Active = 1;
I am unsure how the joins would be done between the tables and the criteria syntax to complete such a task.
For this particular query, the list of Site ids will be passed in
Also the condition “org.PermRead = 1”, the property name will be dynamic. so it could be “PermWrite”.
Edit:
Ok so this is what I have so far
ICriteria criteria = this.Session.CreateCriteria<User>();
criteria.Add(Restrictions.In("User.Site", siteList.ToArray()));
criteria.Add(Restrictions.Eq("User.Active", true));
criteria.Add(Restrictions.Eq("Site.Active", true));
criteria.Add(Restrictions.Eq(Enum.GetName(typeof(Perm.Types), access), true));
criteria.SetResultTransformer(Transformers.DistinctRootEntity);
return criteria.List<User>();
I think a join would be in a format like this:
criteria.CreateAlias("Sites", "Sites", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
I ended up with this: