tags:

views:

89

answers:

1
SELECT *
FROM [Group] g
INNER JOIN User2Group ug
    **on g.Id != ug.GroupId**
INNER JOIN [Activity] a
    on a.Id = g.ActivityId
WHERE g.UserId != 2
AND a.Lineage like '0,1,%'

Group > 1-n > User2Group < n-1 < User m-n relationship

Activity > 1-n > Group 1-n

Trying to get all groups that a user has not already added to their account.

What I have so far:

var groups = repository.SimpleQuery<Group>("from Group as g join fetch g.Users as u join fetch g.Activity as a where g.Type != ? and a.Lineage like ? and g.CreatedBy.Id != ?", Group.GroupType.Deleted, string.Format("{0}%", lineage), currentUser.Id);

What has me tripped up is the "on g.Id != ug.GroupID"

+1  A: 

It's a bit hard when I don't see the entities and the mappings, but the

on g.Id != ug.GroupId

part could probably be expressed in HQL by

from Group as g where g.id not in (select u.group.id from User u where u.id = ?)

The rest of the where clause should be easy to add.

Mind that it's been a while since I worked with HQL :-)

Magne
I was trying a not in within Ayende's QueryBuilder and it wasn't working and didn't try it in HQL. I went to SQL and got that going, but I think your HQL might also work. I'll give it a try. Thanks!
rball