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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:33:17+00:00 2026-05-15T04:33:17+00:00

I introduced a mapping for a business object which has (among others) a property

  • 0

I introduced a mapping for a business object which has (among others) a property called “Name”:

public class Foo : BusinessObjectBase
{
    ...
    public virtual string Name { get; set; }
}

For some reason, when I fetch “Foo” objects, NHibernate seems to apply lazy property loading (for simple properties, not associations):

The following code piece generates n+1 SQL statements, whereof the first only fetches the ids, and the remaining n fetch the Name for each record:

ISession session = ...IQuery query = session.CreateQuery(queryString);
ITransaction tx = session.BeginTransaction();

List<Foo> result = new List<Foo>();
foreach (Foo foo in query.Enumerable())
{
    result.Add(foo);
}

tx.Commit();
session.Close();

produces:

select foo0_.FOO_ID as col_0_0_ from V1_FOO foo0_
SELECT foo0_.FOO_ID as FOO1_2_0_, foo0_.NAME as NAME2_0_ FROM V1_FOO foo0_ 
    WHERE foo0_.FOO_ID=:p0;:p0 = 81
SELECT foo0_.FOO_ID as FOO1_2_0_, foo0_.NAME as NAME2_0_ FROM V1_FOO foo0_ 
    WHERE foo0_.FOO_ID=:p0;:p0 = 36470
SELECT foo0_.FOO_ID as FOO1_2_0_, foo0_.NAME as NAME2_0_ FROM V1_FOO foo0_ 
    WHERE foo0_.FOO_ID=:p0;:p0 = 36473

Similarly, the following code leads to a LazyLoadingException after session is closed:

ISession session = ...
ITransaction tx = session.BeginTransaction();
Foo result = session.Load<Foo>(id);
tx.Commit();
session.Close();

Console.WriteLine(result.Name);

Following this post, “lazy properties … is rarely an important feature to enable … (and) in Hibernate 3, is disabled by default.”

So what am I doing wrong? I managed to work around the LazyLoadingException by doing a NHibernateUtil.Initialize(foo) but the even worse part are the n+1 sql statements which bring my application to its knees.

This is how the mapping looks like:

<class name="Foo" table="V1_FOO">
    ...
    <property name="Name" column="NAME"/>
</class>

BTW: The abstract “BusinessObjectBase” base class encapsulates the ID property which serves as the internal identifier.

  • 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-15T04:33:18+00:00Added an answer on May 15, 2026 at 4:33 am

    I don’t think that this is due to lazy property loading. It’s rather because of the use of Enumerable and Load.

    Take a look at the reference documentation about Enumerable:

    … The iterator will load objects on
    demand, using the identifiers returned
    by an initial SQL query (n+1 selects
    total)
    .

    Either use batch fetching to reduce the number of queries (in the mapping of the class)

    <class name="Foo" table="V1_FOO" batch-size="20">
    

    … or use List instead of Enumerable:

    IQuery query = session.CreateQuery(queryString);
    List<Foo> result query.List<Foo>();
    

    Note: Enumerable only makes sense if you don’t expect that you need the whole result, or in special cases where you don’t want to have them all in memory at the same time (then you need Evict to remove them). For the most cases, List is what you need.


    In the case of Load, only a proxy is created (no query is performed). On the first access to it, it is loaded. (This is very powerful for instance to use this proxy as filter arguments in queries or to link it to another entity without the need of loading its contents.) If you need its contents, use Get instead

    using (ISession session = ...)
    using (ITransaction tx = session.BeginTransaction())
    {
        Foo result = session.Get<Foo>(id);
    
        tx.Commit();
    }
    // could still fail in case of lazy loaded references
    Console.WriteLine(result.Name);
    

    … or even better, use the entity only while the session is open.

    using (ISession session = ...)
    using (ITransaction tx = session.BeginTransaction())
    {
        Foo result = session.Load<Foo>(id);
        // should always work fine
        Console.WriteLine(result.Name);
    
        tx.Commit();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Delphi 8 introduced Class Helpers for the purposes of mapping the VCL/RTL to the
Normally, when you define a mapping for a certain property of a class, NHibernate
Rails introduced some core extensions to Ruby like 3.days.from_now which returns, as you'd expect
I recently introduced HTML into some RSS feeds that I publish (which up to
C++11 introduced variadic templates template <typename... Args> void foo(Args... params) { cout << sizeof...(Args)
WebKit introduced the -webkit-device-pixel-ratio media feature and window.devicePixelRatio JavaScript property, to allow web authors
We're designing a database that has a simple table called Person. Each person can
I'm using the simple object/graph mapping in Spring Data Neo4j 2.0, where I perform
Delphi 2010 introduced custom attributes which can be added to type declarations and methods.
Entity Framework has changed drastically since they have introduced version 1. EF 4.1 has

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.