I have 3 tables I am using to test NHibernate. For the most part, things work, but I'm having a heck of a time with lazy instantiation.
Simple structure is: User has many Notes which have many Comments.
By default, all relations have lazy="true". This works fine. Now, when I want to select all Users and their notes, I set fetch mode="JOIN" on the criteria, and do the select. This works fine to get the Notes. However, I cannot get both the Notes and the Comments in one fetch. If I set the Notes and Comments lasy="false" everything works fine, but that won't scale past my testing.
crit.SetFetchMode("Notes", FetchMode.Join)
crit.SetFetchMode("Notes.Comments", FetchMode.Join)
' Iterate through users here
' Iterate through user.notes here...
' Iterate through note.Comments here <-- Throws error.
I'm using VB.Net, so all methods are virtual, so that isn't the problem. Am I missing something here?
Code:
<class name="HibernateTest.PersistedUser, HibernateTest" table="PUser">
<id name="UserId" column="user_id" type="Int32">
<generator class="native" />
</id>
<property name="Username" column="username" type="String" length="255"/>
<property name="Email" column="email" type="String" length="255"/>
<set name="Notes" table="PNote" order-by="note_id" lazy="true">
<key column="user_id"/>
<one-to-many class="HibernateTest.Note, HibernateTest"/>
</set>
</class>
<class name="HibernateTest.Note, HibernateTest" table="PNote">
<id name="NoteId" column="note_id" type="Int32">
<generator class="native" />
</id>
<property name="UserId" column="user_id" type="Int32"/>
<property name="Text" column="note_text" type="String" length="255"/>
<set name="Comments" table="PComment" order-by="comment_id" lazy="true">
<key column="note_id"/>
<one-to-many class="HibernateTest.Comment, HibernateTest"/>
</set>
</class>
<class name="HibernateTest.Comment, HibernateTest" table="PComment">
<id name="CommentId" column="comment_id" type="Int32">
<generator class="native" />
</id>
<property name="NoteId" column="note_id" type="Int32"/>
<property name="UserId" column="user_id" type="Int32"/>
<property name="Text" column="note_text" type="String" length="255"/>
</class>
Any additional pointers are appreciated.
-S