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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:05:00+00:00 2026-05-11T05:05:00+00:00

I have a table Parent and a table Child. Child contains a foreign-key to

  • 0

I have a table Parent and a table Child. Child contains a foreign-key to the Parent table, creating a one-to-many relationship. Here is a part of my mapping that I define with fluent NHibernate:

public class ParentMap : ClassMap<Parent> {     public ParentMap()     {         WithTable('Parents');          Id(x => x.Id, 'ParentID')         .WithUnsavedValue(0)         .GeneratedBy.Identity();          Map(x => x.Description, 'Description');          HasMany<Child>(x => x.Childs)         .LazyLoad()         .WithKeyColumn('ParentID')         .IsInverse()         .AsSet();     } }  public class ChildMap : ClassMap<Child> {     public ChildMap()     {         WithTable('Childs');          Id(x => x.Id, 'ChildID')         .WithUnsavedValue(0)         .GeneratedBy.Identity();          References(x => x.Parent, 'ParentID')             .CanNotBeNull()             .LazyLoad();     } } 

As you can see I have set LazyLoad on the relation. Note also that in my model classes, all properties are set as virtual.

Now for the simple query:

ICriteria crit = Session.CreateCriteria(typeof(Child))     .Add(Expression.Eq('Id', 18)); IList<Child> list = crit.List<Child>(); 

And the SQL generated:

SELECT this_.ChildID            as ChildID5_1_,        this_.ParentID           as ParentID5_1_,        parent2_.ParentID    as ParentID4_0_,        parent2_.Description as Descript2_4_0_ FROM   Childs this_        inner join Parents parent2_          on this_.ParentID = parent2_.ParentID WHERE  this_.ChildID = 18 /* @p0 */ 

As you can see, it does a join on the Parent table and selects its fields (id and description). But why does it do that since I requested lazyloading ?

Now if I change the query to:

ICriteria crit2 = Session.CreateCriteria(typeof(Child))     .SetFetchMode('Parent', FetchMode.Lazy)     .Add(Expression.Eq('Id', 18)); 

There are 2 sql queries generated:

SELECT this_.ChildID  as ChildID5_0_,        this_.ParentID as ParentID5_0_ FROM   Childs this_ WHERE  this_.ChildID = 18 /* @p0 */ 

which is good to me: no join, the Parent table is not queried. But I get this second one too:

SELECT parent0_.ParentID    as ParentID4_0_,        parent0_.Description as Descript2_4_0_ FROM   Parents parent0_ WHERE  parent0_.ParentID = 45 /* @p0 */ 

which again queries the Parent table.

These 2 queries are generated during the line:

IList<Child> list = crit.List<Child>(); 

I’m totally ignorant of what happens here. Can someone help?

  • 1 1 Answer
  • 1 View
  • 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. 2026-05-11T05:05:00+00:00Added an answer on May 11, 2026 at 5:05 am

    It’ll depend on your version of Fluent NHibernate. Up until a certain point it was the default that all entities would not be lazy loaded. This is the equivalent of explicitly setting lazy='false' in your entity. This is no longer the case, but if you are running on anything prior to that point then you’ll see this behaviour.

    The many-to-one/references lazy load setting gets overridden by the entity level lazy load from the target, so if you are running on this older version of FNH then the entity setting will be rendering your References(...).LazyLoad() call moot.

    You need to verify you’re on the latest version of FNH, that should fix things; however, if it doesn’t then you need to explicitly set lazy loading on in your Parent entity. You can do that with the LazyLoad method on the ClassMap<T>.

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

Sidebar

Related Questions

I have db table with parent child relationship as: NodeId NodeName ParentId ------------------------------ 1
I have a parent-child relationship: @Entity @Table(name = user) public final class User {
I have two parent tables, BusinessGroup and SocialGroup, and one child table, Members. A
I have a child table that contains the history of statuses for the particular
I have a parent child relationship between 2 classes Parent @Entity @Table(name = PARENT)
Have an interesting situation: I have a foreign key from a.name (child table) to
I have a table with a column named Description that contains XML like: <Parent>
I have two tables, Parent and Child. The column Parent.favorite_child has a foreign key
I have a database table with parent child relationships between different rows. 1 parent
I have a simple parent/child tables. Contract is the parent table. Each contract can

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.