I'm using NHibernate 3.3.3.4001. I'm having an issue where NHibernate is not lazy loading data when issuing a Get request. Instead it is populating the entire object model resulting in very slow performance. The .hbm file is as follows:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="My.Assembly" namespace="Assembly.Model">
<class name="Parent" table="tblParent">
<id name="ID">
<generator class="native"></generator>
</id>
<version name="Version" column="Version"/>
<component name="Children">
<bag name="Collection" table="tblChildren"
cascade="save-update, merge" inverse="true">
<key column="ParentID"></key>
<one-to-many class="Children"></one-to-many>
</bag>
</component>
<property name="DateCreated" column="DateCreated" update="false" insert="false" />
<property name="Inactive" column="Inactive" />
</class>
</hibernate-mapping>
I had expected NHibernate to load all child objects lazily and not send queries to the database. I have tried adding explicit lazy = true and default-lazy=true but it made no difference. I have inspected the Config object and can see that the mappings have islazy=true.
I am calling Get as follows:
using (var transaction = Session.BeginTransaction())
{
t = Session.Get<T>(id);
transaction.Commit();
}
I am puzzled as to why this is not loading lazily but is querying the database for all the child objects?