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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T21:52:54+00:00 2026-05-19T21:52:54+00:00

I have a Project and a Person (somewhat simplified): public class Project { public

  • 0

I have a Project and a Person (somewhat simplified):

public class Project
{
    public virtual Person CreatedBy { get; set; }
}

public class Person
{
    public virtual string Username { get; set; }
}

I’m getting a Project with NH Linq:

var projects = from p in session.Query<Project>()
             select p;
var project = projects.First();

and sometimes with Fetch()

var project = projects.Fetch(p => p.CreatedBy).First();

Now, how can I verify in a unit test that project.CreatedBy is not or is eagerly loaded (and the reverse), depending on if Fetch() was used? In my understanding, using Fetch() should eager load the Person and if I don’t use Fetch() it should lazy load the Person.

I’ve tried

Assert.IsTrue(NHibernateUtil.IsInitialized(project.CreatedBy));

And

NHibernateUtil.IsPropertyInitialized(project, "CreatedBy")

But they both return true whether I use Fetch() or not. IsInitialized() works fine with collections (one-to-many), for instance Person.Projects, but not with many-to-one…

The CreatedBy property should be lazy loaded but sometimes I want it to be eagerly loaded and I want unit tests confirming that it’s working as intended.

The mapping for the CreatedBy property is

<many-to-one name="CreatedBy" class="Person" fetch="select">
  <column name="PERSON_ID" precision="10" scale="0" not-null="true" />
</many-to-one>

I verified the mapping like this:

var to1 = NHibernateConfiguration.GetClassMapping(typeof(Project))
    .ReferenceablePropertyIterator.FirstOrDefault(p => p.Name == "CreatedBy")
    .Value as NHibernate.Mapping.ToOne;

Assert.IsTrue(to1.IsLazy);
Assert.IsFalse(to1.UnwrapProxy);

Which passes.

Any ideas?

  • 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-19T21:52:54+00:00Added an answer on May 19, 2026 at 9:52 pm

    If NHibernateUtil.IsInitialized(project.CreatedBy) returns true, it’s because it is initialized.

    If the Person has already been loaded in the session, it will be initialized regardless of whether you fetch CreatedBy eagerly or not, thanks to the Identity Map.

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

Sidebar

Related Questions

If I have three entities, Project, ProjectRole and Person, where a Person can be
I have just seen the news that a person has left Apple to develop
I have a small project that I will be working on shortly that collects
I have a new project starting up, and I was planning to use git
I am looking into migrate a large project to Entity Framework 4.0 but am
I've got a medium scale project (a turn-based game) that's currently written in C/C++.
I needed to do some higher-level project planning that doesn't really fit into the
I'm writing an editor for large archive files (see below) of 4GB+, in native&managed
I use Eclipse to write Java code and use DropBox to sync my code
I understand how to design a database schema that has simple one-to-many relationships between

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.