Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4597874
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:19:09+00:00 2026-05-21T23:19:09+00:00

I have a many-to-many relationship which I am trying to query. My problem is

  • 0

I have a many-to-many relationship which I am trying to query. My problem is very similar to one detailed by Phillip Haydon here so I am going to liberally borrow his diagrams and explanation.

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

Phillip needed to query all the roles which were not assigned to a job. His solution was the following:

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 helpful however it appears that in this solution there is an entity for each table; Job, JobRole, and Role. JobRole has both a Job and a Role. Probably 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;}
}

This conflicts with the pattern I have seen for modeling many-to-many relationships, specifically in sharp architecture examples and from advice here. In those examples and in my case, I have only two classes, Job and Role. 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 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;}
}

In my case, I need to find all jobs which only have roles. I’ve tried something like this

  Job job = null;
  Role role = null;

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

but NHibernate requires that the projection for the select in the WhereExists and complains if one is not provided, which makes sense to me.

With my model is it even possible to do a QueryOver Subquery with WhereExists?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T23:19:09+00:00Added an answer on May 21, 2026 at 11:19 pm
    var jobs = session
         .QueryOver<Job>()
         .WhereRestrictionOn(j => j.Roles)
             .IsNotEmpty
         .List();
    

    Mapping for Job:

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

    and mapping

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

    In my case produce following SQL (beautified):

    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)
    

    and returns only jobs with roles as you want

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a many-to-many relationship between invoices and credit card transactions, which I'm trying
I have 2 entities: Faculty and Course which are in a one-to-many relationship, that
I have a JPA object which has a many-to-many relationship like this: @Entity public
I have a table which has several one to many relationships with other tables.
I have two models, Project and Category, which have a many-to-many relationship between them.
I am trying to set up a simple one-to-many relationship where a list can
i have two entities named Parent and Child , linked in a one-to-many relationship.
I have a notes model, which has a many-to-many relationship with the users model.
So I'm trying to fetch data in a many-to-many relationship. So far I have
I have two tables, Clients and Administrators, which are linked in a many-to-many relationship

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.