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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:25:49+00:00 2026-05-12T12:25:49+00:00

I’ve either misunderstood the NHibernate manual or I’ve done something wrong. Can anyone help?

  • 0

I’ve either misunderstood the NHibernate manual or I’ve done something wrong. Can anyone help?

I am trying to retrieve a User without the AuditLogEntrys.
But NHibernate is still loading the AuditLogEntrys. I only want the AuditLogEntrys loaded when I access the property.

public class User
{
    public virtual int UserId { get; set; }
    public virtual string UserName { get; set; }
    public virtual IList<AuditLogEntry> AuditLogEntrys { get; set; }
}

public class AuditLogEntry
{
    public virtual int Id { get; set; }
    public virtual DateTime DateRead { get; set; }
    public virtual string MachineName { get; set; }
}

Mappings:

  <class name="Model.User, Model" 
     table="User" 
     lazy="true">
  <id name="UserId" access="property" column="UserID">
    <generator class="native"></generator>
  </id>
  <property name="UserName" access="property" />
  <bag name="AuditLogEntrys" lazy="true" access="property">      
    <key column="UserID" />      
    <one-to-many class="Model.AuditLogEntry, Model"></one-to-many>
  </bag>

  <class name="Model.AuditLogEntry, Model"
     table="AuditLog"
     lazy="true">
    <id name="Id" access="property" column="ID">
      <generator class="native"></generator>
    </id>        
    <property name="DateRead" access="property" column="DateRead"></property>
    <property name="MachineName" access="property" column="MachineName"></property>    
  </class>

Code to get the user:

  public IList<User> GetUserByUserName(string userName)
  {
      ICriteria criteria = NHibernateSession.CreateCriteria(typeof(User))
          .Add(Expression.Eq("UserName", userName));

      return GetByCriteria(criteria);
  }

Now I’d expected a User object with an empty collection of AuditLogEntry’s, but this is not what is happening.

Any ideas??
Thanks.

  • 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-12T12:25:49+00:00Added an answer on May 12, 2026 at 12:25 pm

    With lazy loading, you will get a populated list of objects, but they are not yet “hydrated” from the database. The lazily-loaded objects are not your entity types, but are instead “proxy objects” which will be populated/hydrated with real data when you access the items in the collection.

    The use of proxy objects is the reason why you have to make all of your properties virtual in your entity types. The Proxy types are dynamically-generated subclasses of your entity type, which make the actual calls to the database when you access the properties.

    Hopefully I understood your question, but the difference is that you get actual objects back, not an empty list. If you get back an empty list, it means that there were no AuditLogEntry items referencing your User in the database.

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

Sidebar

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.