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

  • SEARCH
  • Home
  • 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 6228965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:28:52+00:00 2026-05-24T09:28:52+00:00

I am using Nhibernate for my ORM. I have a class Control that has

  • 0

I am using Nhibernate for my ORM.

I have a class “Control” that has a one to many relationship with ControlDetail (ie. A control has many controlDetails).

In the control xml config it has the following

<bag name="ControlDetails" lazy="true" access="property" order-by="SortOrder asc"  cascade="all-delete-orphan" 
  table="ControlDetail">
  <key column="ControlID"/>
  <one-to-many class="ControlDetail"/>
</bag>

such that I believe unless otherwise told it would lazy load the controldetails of a control.

I am running NHProf to try and fix some performance issues we are having and it has identfied a Select N + 1 issue around these classes.

We are using a repository DA layer and I have tried to see if I can add in a way to eagerly fetch the data when required and came up with this.

public T GetById<T>(Int32 id, List<string> fetch) where T : BaseObject
{
    T retObj = null;
    ISession session = EnsureCurrentSession();
    {
        ICriteria criteria = session.CreateCriteria(typeof (T));
        criteria.SetCacheable(true);
        criteria.Add(Expression.Eq("Id", id));

        foreach(var toFetch in fetch)
        {
            criteria.SetFetchMode(toFetch, FetchMode.Eager);
        }

        retObj = criteria.List<T>().FirstOrDefault();
    }

    return retObj;
}

*Note: I’m not a fan of how the repository is setup but it was done before I came to the project so we have to stick with this pattern for now.

I call this method like so

public Control GetByIDWithDetail(int controlID)
{
    return DataRepository.Instance.GetById<Control>(controlID, new List<string>() {"ControlDetail"});
}

When I debug the GetByID method and look at the retObj I can see that the ControlDetails list has been populated (although strangely enough I also noticed without the setfetchmode set the list was being populated)

Even with this fix NHProf identifies a Select N+1 issue with the following line

List<ControlDetail> details = control.ControlDetails.ToList();

What exactly am I missing and how can I stop this N+1 but still iterate over the controlDetails list

EDIT: the xml configs look like so (slightly edited to make smaller)

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="DomainObjects" assembly="DomainObjects">
    <class name="Control" lazy="false" table="Control" optimistic-lock="version" select-before-update="true"  >
        <id name="Id" type="int" column="ControlID" access="property">
            <generator class="native" />
        </id>
    <version name="Version" column="Version" />
        <property name="AdministrativeControl" column="AdministrativeControl" access="property" not-null="true" />
    <property name="Description" column="ControlDescription" access="property" />
    <property name="Title" column="Title" access="property" />
    <property name="CountOfChildControls" access="property" formula="(select count(*) from Control where Control.ParentControlID = ControlID)" />

    <bag name="ControlDetails" lazy="true" access="property" order-by="SortOrder asc"  cascade="all-delete-orphan"
      table="ControlDetail">
      <key column="ControlID" />
      <one-to-many class="ControlDetail"  />
    </bag>

  </class>
</hibernate-mapping>

and this

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="DomainObjects" assembly="DomainObjects">
    <class name="ControlDetail" lazy="false" table="ControlDetail" select-before-update="true" optimistic-lock="version">
        <id name="Id" type="int" column="ControlDetailID" access="property">
            <generator class="native" />
        </id>
    <version name="Version" column="Version" />
    <property name="Description" column="Description" access="property" not-null="true" />
    <property name="Title" column="Title" access="property" />

    <many-to-one name="Control" lazy="false" class="Control" column="ControlID" access="property"/>
  </class>
</hibernate-mapping>
  • 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-24T09:28:52+00:00Added an answer on May 24, 2026 at 9:28 am

    There is a substantial difference between eager fetching and eager loading. Fetching means: putting into the same query. It has several side effects and may break it. Eager loading means forcing NH to load it immediately, not waiting until it is accessed the first time. It is still loaded using additional queries, which leads to the N+1 problem.

    NH probably does not eagerly fetch because the property is called ControlDetails, but you pass ControlDetail as argument.

    On the other side, this isn’t a good way to avoid the N+1 problem. Use batch-size instead. It is fully transparent to the application and reduces the amount of queries by the given factor (values from 5 to 50 make sense, use 10 if you don’t know what to use).

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

Sidebar

Related Questions

I am using NHibernate as my ORM solution and have a need for a
Using NHibernate.Mapping.Attributes, I have a entity class with something like: [Class] public class EntityA
I have a Data project that has all my nhibernate mappings, and my Repositories
Using NHibernate from C# and only HQL (not SQL) in a way that is
I'm using NHibernate 2 and PostgreSQL in my project. SchemaExport class does a great
I'm using ActiveRecord / NHibernate for ORM mapping in my project with PostgreSQL 8.4.
I'm using nhibernate as my ORM and Firebird embedded as the database. How would
I'm using NHibernate as my ORM and I'm trying to sort some data. The
Using SQL Server 2008, I have a requirement that email addresses in my user
At my company we have a database with one table being so big that

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.