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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:34:36+00:00 2026-06-04T15:34:36+00:00

I have on top entity with many child entities. Now, I want implement FindAll()

  • 0

I have on top entity with many child entities.

Now, I want implement FindAll() method where I’m using this pattern with querying all my top entities to List. That’s fine but I then get all my child objects with it, Alert: Select N+1 in nhib. profiler.

After I get all my objects I send them to FromMyDomainModel method to extract values to ViewModel I need.

   using (ITransaction transaction = session.BeginTransaction())
   {
       List<Property> data =
                   session.Query<Property>()//I don't need fetch
                   .ToList();
        transaction.Commit();
        return EntityViewModel.FromDomainModel(data);
    }

Maybe there is better pattern so please feel free to post.

update: mapping code
from mapping you can figure it my entity code

   public PropertyMap()
        {
            Table("Property");
            Id(x => x.Id).GeneratedBy.Identity();
            Map(x => x.Version);
            Map(x => x.Created);
            Map(x => x.Updated);
            Map(x => x.Views);
           ....
            Map(x => x.Price);            
            HasMany(x => x.Photos).KeyColumn("PropertyId").Cascade.All();
        }

 public PhotoMap()
        {
            Table("Photo");
            Id(x => x.Id).GeneratedBy.Identity();
            Map(x => x.ImageData).CustomSqlType("VARBINARY(MAX)").Length(160000).Not.Nullable();
            Map(x => x.ImageMimeType).Not.Nullable();
            References(x => x.Property).Column("PropertyId");

        }
  • 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-04T15:34:38+00:00Added an answer on June 4, 2026 at 3:34 pm

    To reduce the select n+1 problem you need to set the batch-size

    NH3.2+ sets loading batch-size to 20 by default so if you can upgrade then I would recommend it. See here [NH-2593]

    If you cannot upgrade and if are using XML mappings then do something like:-

    <bag/set/list ... batch-size='50' ..>
    ...
    </bag/set/list>
    

    If you are not using XML mappings then please post a mapping example.

    edit based on your updated mapping

    Try this and reprofile, you should see your n+1 reduce by a magnitude of 50

    HasMany(x => x.Photos).KeyColumn("PropertyId")
      .SetAttribute("batch-size", "50").Cascade.All();
    

    Also you can set this batch-size in the configuration before you build your session factory so it applies to all your collection fetching.

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

Sidebar

Related Questions

I have the following (simplified) Hibernate entities: @Entity @Table(name = package) public class Package
I have a simple LINQ query running on top of Entity Framework (v1) and
While I try to save top level entity (using JPA), do I need to
I'm using MVC3 to build a web application. In this application I have an
I have following Entity Framework Logic, and I want to translate to Stored Procedure,
I have a client who has a web service providing several different top-level entities.
I have a CoreData model that looks like this: Abstract Entity Entity 1 Entity
I have a object structure like this: public class Entity { IList<Relationship> Relationships{get;set;} }
I have a entity framework driven solution, on top of which, I have a
Lets say I have the following in my top level .vhd file entity toplevel

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.