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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:26:51+00:00 2026-05-29T10:26:51+00:00

How to load complete object (that has all asociated objects within) using Fluent NHibernate?

  • 0

How to load complete object (that has all asociated objects within) using Fluent NHibernate? In this case the object is named Project and has associated Category and list of Pictures. Mapping file looks like this:

        public ProjectMap()
    {
        Id(x => x.Id);

        Map(x => x.Title).Not.Nullable();
        Map(x => x.Year);
        Map(x => x.Slug).Not.Nullable();
        Map(x => x.Description).CustomSqlType("nvarchar(MAX)").Not.Nullable();
        References(x => x.Category, "CategoryId").Not.Nullable();
        HasMany(x => x.Gallery)
            .Inverse()
            .Cascade.All();
    }

And repository method that should return the complete object:

public Project Read(int id)
    {
        using (var session = NHibernateHelper.OpenSession())
        {
            var project = session.CreateQuery("FROM Project WHERE Id = :Id LEFT JOIN FETCH p.Category LEFT JOIN FETCH p.Gallery")
                .SetParameter("Id", id).UniqueResult<Project>();
            return project;
        }
    }
  • 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-29T10:26:52+00:00Added an answer on May 29, 2026 at 10:26 am

    I’m assuming you want to lazy load by default, but sometimes you want eager loading when necessary. If you always want to eager load (not recommended), you should do that in the mapping instead with Not.Lazyload and choosing a fetch strategy with .Fetch().

    Otherwise, you will have to use one of the query methods in NHibernate to do what you want. You can do this with an HQL query, or Linq query, and probably with a Criteria query.

    Here’s a LINQ query (this works in 3.2, not sure if it’s available before then)

    session.Linq<Project>()
        .Fetch(p => p.Category)
        .FetchMany(p => p.Gallery).FirstOrDefault();
    

    Here’s an HQL version

    session.CreateQuery("SELECT p FROM Project p FETCH JOIN p.Category FETCH JOIN p.Gallery")
        .UniqueResult<Project>();
    

    Here’s a Criteria version

    session.CreateCriteria<Project>("p")
        .SetFetchMode("p.Category", FetchMode.Join)
        .SetFetchMode("p.Gallery", FetchMode.Join)
        .UniqueResult<Project>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Once all three loads are complete... $(#sidebar a).live(click, function(e){ $(this).addClass(selected loading); $(#conceptual).load(conceptualUrl, null, function(){
I have a problem with hashCode() that delegates to uninitialized objects using hibernate. My
Is there such a thing as an on load complete for images in iphone?
To complete some testing I need to load the 64 bit version of an
While trying to load a Bitmap onto a SWFLoader the Event.COMPLETE event is not
What exactly is DOM Load time? Is it time to just get complete html
I load data into my grid using a WCF service. When a user clicks
I load a UIWebView from a string in a navigation based app. I'm using
I have an object that loads an instance of another object in a method.
I'm using the following code to load an swf in a pure actionscript project

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.