I have a 1:m relationship between these 2 tables: Application and Role. In my Application class I have a Roles collection. I have lazy loading turned on by default for the Roles collection but in one instance I want to eagerly load the collection using this code:
ICriteria crit = session.CreateCriteria(typeof(Application));
crit.SetFetchMode("Roles", FetchMode.Select);
IList<Application> user = crit.List<Application>();
The Application is fetched fine but the Roles collection is still being lazy loaded. I profiled the DB calls and the Application is loaded but the Roles are not until I access the collection. What am I doing wrong?
The mapping files follow:
Code:
<hibernate-mapping default-cascade="none" xmlns="urn:nhibernate-mapping-2.2" assembly="xxx" namespace="xxx">
<class name="Application" table="Application">
<id name="ID" type="System.Int32" column="ApplicationId" unsaved-value="0">
<generator class="identity" />
</id>
<!-- Properties -->
<property name="ApplicationName" type="System.String" column="ApplicationName" not-null="true" length="20" />
<property name="Title" type="System.String" column="Title" not-null="true" length="50" />
<property name="Description" type="System.String" column="Description" not-null="true" length="50" />
<property name="IsPublished" type="System.Boolean" column="IsPublished" not-null="true" />
<property name="URL" type="System.String" column="URL" not-null="true" length="20" />
<property name="ModifiedDate" type="System.DateTime" column="ModifiedDate" not-null="true" />
<property name="ModifiedBy" type="System.String" column="ModifiedBy" not-null="true" length="20" />
<!-- Relationships -->
<bag name="Roles" inverse="true" lazy="true" fetch="select" outer-join="false">
<key column="ApplicationId" />
<one-to-many class="Role"/>
</bag>
</class>
</hibernate-mapping>
Code:
<hibernate-mapping default-cascade="none" xmlns="urn:nhibernate-mapping-2.2" assembly="xxx" namespace="xxx">
<class name="Role" table="Role">
<id name="ID" type="System.Int32" column="RoleId" unsaved-value="0">
<generator class="identity" />
</id>
<!-- Properties -->
<property name="RoleName" type="System.String" column="RoleName" not-null="true" length="20" />
<property name="Description" type="System.String" column="Description" not-null="true" length="50" />
<!-- Relationships -->
<!-- m:1 relationship between Role and Application -->
<many-to-one name="Application" class="Application" lazy="false" fetch="select" column="ApplicationId" />
<!-- m:n relationship between Role and NewUser -->
<bag name="NewUsers" table="NewUserRole" inverse="true" lazy="true" fetch="select" outer-join="false">
<key column="RoleId" />
<many-to-many class="NewUser" column="NewUserId" />
</bag>
</class>
</hibernate-mapping>