I'm aware that lazy-loading 'on' is the default setting in NHibernate. I switched lazy-loading off, using mapping-by-code, for the entity (Student) and the collection (Comments) contained within an entity. However a test including the use of SQL-Profiler shows that it does not load the collection from the database when the entity is accessed via a Session.Get(). I see only a 'Select' to get the entity (Student) from the Db. No 'Join' or 'Selects' to the collection table (Comments). Am I missing something? I'm using NH version 5.
Mapping:
using NHibernate.Mapping.ByCode.Conformist;
using NHibernate.Mapping.ByCode;
namespace Infrastructure.Repository.NH.Tests
{
public class StudentSubclassMapping: JoinedSubclassMapping<Student>
{
public StudentSubclassMapping()
{
Lazy(false);
Property(student => student.EnrollmentDate);
List(student => student.Comments,
listMapper =>
{ listMapper.Lazy(CollectionLazy.NoLazy);},
relationMapper =>
relationMapper.Element());
}
}
}
Domain:
public class Student : Contact
{
public virtual DateTime? EnrollmentDate { get; set; }
public virtual IList<string> Comments { get; set; }
}
Test:
public void Get_TestToCheckIfLazyLoadingIsTurnedOff()
{
using (var session = SessionFactory.OpenSession())
{
using (var transaction = session.BeginTransaction())
{
var student = session.Get<Student>(2);
transaction.Commit();
}
}
}