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 8576979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:03:28+00:00 2026-06-11T20:03:28+00:00

I have used nHibernate in my latest project and successfully mapped all the basic

  • 0

I have used nHibernate in my latest project and successfully mapped all the basic relationships where the values existed in either the primary table I was working with or through a simple relationship like a composite.

Where I am getting stuck is how does one go about mapping complex joins?

For example I have an entity called Contact each contact has your usual properties like Name, DOB, Phone…. But I also need it to have a property called AccreditationList which would be a List<Accreditation>.

Here is a sample of the Contact XML declaration.

<class name="Contact" table="Contact" lazy="true">

    <id name="ID" column="ContactID" type="guid">
      <generator class="guid" />
    </id>

    <property name="FirstName">
      <column name="FirstName" sql-type="nvarchar(500)" not-null="true" />
    </property>

    <property name="LastName">
      <column name="LastName" sql-type="nvarchar(500)" not-null="true" />
    </property>

    <bag name="AccreditationList" lazy="true">    
        //How do I express the relationship here? 
    </bag>

</class>

The List<Accreditation> can only be determined through a series of joins like this.

SELECT Accreditation.* FROM CourseEnrolment 
INNER JOIN Course ON Course.CourseID = CourseEnrolment.CourseID
INNER JOIN CourseType ON CourseType.CourseTypeID = Course.CourseTypeID
INNER JOIN Accreditation ON Accreditation.AccreditationID = CourseType.AccreditationID
WHERE CourseEnrolment.ContactID = :ContactID

Is the only way to accomplish this by manually invoking the SQL through nHibernate in code with CreateSQLQuery or can I express this relationship using something like named queries? What is the correct approach? Any guidance would be appreciated.

  • 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-06-11T20:03:29+00:00Added an answer on June 11, 2026 at 8:03 pm

    I see a couple of options:

    1. You can use SqlSelect, SqlUpdate etc.. to specify an arbitrary SQL statement for selecting / updating etc.. (a mapping-by-code example; I’m sure there’s an XML equivalent)

    2. You could map your SQL query to a QueryOver query, and use it to initialize your Accreditation collection. Something along the lines of:

      public Contact GetContact(int id)
      {
      var contact = _session.Get(id);
      contact.AccreditationList = _session.QueryOver<Accreditation>() /* your query here */;
      return contact;
      }

      However!! this method has several drawbacks-

      • If you don’t use the GetContact method, your collection won’t be populated correctly.
      • It doesn’t support Querying over Contact very easily (first you have to query for the contacts, and then you have to initialize each contact’s accreditation list).
    3. You could map the intermediate entity (Course) to your Contact entity (you can have it as a private member, if you don’t want to expose them), and then your AccreditationList property would be

    public IEnumerable<Accreditaion> AccreditationList
    {
    get
    {
    return _courses.SelectMany(course => course.Accreditations);
    }
    }

    You wouldn’t be able to manipulate the Contact‘s AccreditationList directly, though.

    The best solution would be to have something like QueryOverSelect option in the mapping, but AFAIK there isn’t…
    Personally I’d go with the 3rd option, since it seems the cleanest to me.

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

Sidebar

Related Questions

In a upcoming project I'm going to use NHibernate with Oracle. I have used
I have one project using NHibernate 1.2. I know it's old, but I've used
Say I have an application that used an ORM (I am thinking of nHibernate
I have a MySQL database includes a table named 'Task'. and I used Hibernate
I have downloaded the Fluent NHibernate source distribution, compiled all projects in solution and
I used in my project NHibernate like this: public class DBHelper { private static
Please if you have used NHibernate and Entity Frameworks, please contrast your experiences.
I am using IQueryable<> to build up batching queries. I have used views successfully
I have used both NHibernate Futures and NHibernate CreateMultiCriteria in the past to batch
During my apprenticeship, I have used NHibernate for some smaller projects which I mostly

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.