tags:

views:

121

answers:

2

Trying to do a bit more complex query, and thought that HQL would be better for the job. Using nHibernate.

SELECT * FROM [Group] g 
  INNER JOIN [User2Group] ug on g.Id = ug.GroupId
  INNER JOIN [User] u ON u.Id = ug.UserId
  INNER JOIN Activity a on g.ActivityId = a.Id
WHERE u.Id = ? AND a.Lineage LIKE '?%'

I guess I could also just use the SQL as well (?), but not sure really how to load up my objects that way.

+1  A: 

It would depend on what your entities are and what the primary object you care about is. You seem to be pulling them all instead of just one entity. I am going to assume Group is the entity from this example

from MyApp.Entities.Group as g
join fetch g.Users as u
join fetch g.Activity as a
where u.Id = :userId and a.Lineage like '?%'

That should get you started. But with out knowing your structure, I am taking a shot in the dark.

Nick Berardi
Sorry, you are correct, I'm trying to get a collection of groups.
rball
I think you're pretty close, I'll give it a shot and get back with you. Thanks for the answer.
rball
Got me to where I needed to be. What I ended up using: var groups = repository.SimpleQuery<Group>("from Group as g join fetch g.Users as u join fetch g.Activity as a where u.Id = ? and a.Lineage like ?", currentUser.Id, string.Format("{0}%", lineage));
rball
A: 

If you have many-to-many connection (User2Group table) you should start from it and make all the joins on this table. If you have correct mappings, the query below will work like a charm.

from User2Group as ug
join ug.User as u
join ug.Group as g
where u.Id = :userId and g.Activity.Lineage like '?%'
Maras
I have a [HasAndBelongsToMany(typeof(Group), Table = "User2Group", ColumnKey = "UserId", ColumnRef = "GroupId")] public virtual IList<Group> Groups and similar from User to Group many to many. I originally started with something like this but my end result needs to be a collection of groups.
rball