views:

708

answers:

1

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  A: 

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.

Andy White
Wouldn't you get a lazy collection first off which when accessed would go and get the entries?
ShaneC
ShaneC - Yes, that's how I thought it would work.
empo
Andy, that kinda makes sense to me. So how come I can see the data when inspecting the AuditLogEntrys property of the User object? Is it because Nhibernate is populating it then?
empo
Ignore that last comment. I just checked for myself and saw in SQL Profiler that the first query is only getting the User. When I inspect the value of the AuditLogEntrys property, another query is executed. Thanks for the explanation.
empo