NHibernate QueryOver WhereExists for many-to-many

I have a many-to-many relationship that I am trying to fulfill. My problem is very similar to what Philip Haydon described here , so I'm going to liberalize his diagrams and explanations.

Phillip had the following many-to-many relationship between Jobs and Roles (sorry I can't insert images yet):
data schema

Phillip needed to request all the roles that were not assigned to the task. His solution was as follows:

Role roleAlias = null;

var existing = QueryOver.Of<JobRole>()
                    .Where(x => x.Job.JobId == jobId)
                    .And(x => x.Role.RoleId != roleId)
                    .And(x => x.Role.RoleId == roleAlias.RoleId)
                    .Select(x => x.Role);

result = Session.QueryOver<Role>(() => roleAlias)
            .Where(x => x.IsEnabled)
            .WithSubquery.WhereNotExists(existing)
            .OrderBy(x => x.Name).Asc
            .List();

This was very useful, but it turned out that in this solution there is an entity for each table; Job, JobRole and Role. JobRole has both a job and a role. Maybe something like this:

public class Job
{
  public int JobId {get;set;}
  public string Name {get; set;}
  public bool IsEnabled {get;set;}
  public bool IsDefault {get;set;}
}

public class Role
{
  public int RoleId {get;set}
  public string Name  {get;set}
  public bool IsEnabled {get;set}
  public string RoleType {get;set}
}

public class JobRole
{
  public Job Job {get;set}
  public Role Role {get;set;}
}

, " ", , . : " ". - :

public class Job
{
  public int JobId {get;set;}
  public string Name {get; set;}
  public bool IsEnabled {get;set;}
  public bool IsDefault {get;set;}
  public IList<Role> Roles {get;set;}
}

public class Role
{
  public int RoleId {get;set}
  public string Name  {get;set}
  public bool IsEnabled {get;set}
  public string RoleType {get;set}
  public List<Job> Jobs {get;set;}
}

, . -

  Job job = null;
  Role role = null;

  var jobs = Session.QueryOver(() => job)
                    .WithSubquery
                    .WhereExists(
                          QueryOver.Of(() => role)
                          .JoinAlias(() => role.Jobs, () => job))
                    .List().ToList();

NHibernate , select WhereExists , , .

QueryOver Subquery WhereExists?

.

+3
1
var jobs = session
     .QueryOver<Job>()
     .WhereRestrictionOn(j => j.Roles)
         .IsNotEmpty
     .List();

:

 public class Job
    {
        public virtual int ID { get; set; }
        public virtual string Name { get; set; }
        public virtual IList<Role> Roles { get; set; }
    }

 <class name="Job">
       ...
       <bag name="Roles" table="JobRoleMap" fetch="join" lazy="false" >
          <key column="JobID" />
          <many-to-many column="RoleID" class="Role" />            
       </bag>
    </class>

SQL ():

SELECT j.ID,
       j.Name,
       roles.JobID,
       r.ID,
       r.Name
FROM   Job j
       left outer join JobRoleMap roles
         on j.ID = roles.JobID
       left outer join Role r
         on roles.RoleID = r.ID
WHERE  exists (select 1
               from   JobRoleMap
               where  j.ID = JobID)

,

+7
source

All Articles