views:

39

answers:

1

I have a (fictional) class with Fluent-mapping:

public class Customer 
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual Employee Responsible { get; set; }
    public virtual IList<Order> Orders { get; set; }
}

public class CustomerMapping : ClassMap<Customer    
{
    public CustomerMapping()
    {
        Id(c => c.Id);
        Map(c => c.Name);
        References(c => c.Responsible);
        HasMany(c => c.Orders); 
    }
}

Now - if I fetch a customer from database the HasMany-reference is Lazyloaded, but the References-reference seems not to be lazy loaded. Is this expected? Do I need to explicitly it?

var fromDb = Session.Get<Customer>(id); 
Assert.That(!NHibernateUtil.IsInitialized(fromDb.Orders));
Assert.That(!NHibernateUtil.IsInitialized(fromDb.Reponsible)); // <-- fails
+2  A: 

References (many-to-one) are lazy loaded by default. My bet is that you previously loaded the Responsible object in the same session and it was retrieved from first level cache rather than the database.

Jamie Ide
Thx. Yeah - that's what I thought.. I don't load Responsible any more than I load Orders elsewhere in the session, but I'll try to isolate the test into a separate session..
stiank81