Question

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"

Was it helpful?

Solution

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 :-)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top