I have the following objects as Parent(Model) and Child(Folder). Folder 
again has its own children and so on. I have not added that in the example 
though.


  <class name="Model" table="Model" lazy="true" >
    <id name="Id">
      <generator class="hilo">
        <param name="table">hibernate_unique_key</param>
        <param name="column">NextHi</param>
        <param name="where">TableName='Model'</param>
        <param name="max_lo">100</param>
      </generator>
    </id>
    <property name="Name" not-null="true" />
    <property name="Description" />

*    <bag name="Folders" inverse="true" cascade="all">
        <key column="ModelId" not-null="true" />
        <one-to-many class="Folder" />
    </bag>*

  </class>



  <class name="Folder" table="Folder" dynamic-update="true">
    <id name="Id" >
      <generator class="hilo">
        <param name="table">hibernate_unique_key</param>
        <param name="column">NextHi</param>
        <param name="where">TableName='Folder'</param>
        <param name="max_lo">100</param>
      </generator>
    </id>
    <property name="Name" not-null="true" />
    <property name="Icon" not-null="true" />
    <property name="Description" />

*    <many-to-one update="false" not-null="true" name="Model" 
cascade="none">
      <column name="ModelId" />
    </many-to-one>*

  </class>

My application is sprayed across separate layers(presentation, service, 
business and dataaccess). So effectively the session is per-request 
session. 
My problem was that I was getting a LazyInitializationException when I 
treid to access the Model Objects Folder property which is basically a 
collection 
from the business layer.

I found from NHibernate document that this is a common error and thereby 
tried using fetch(for eager loading) and NhibernateUtil.Initialize()

    <bag name="Folders" inverse="true" cascade="all" *fetch=select and 
lazy= false*>
        <key column="ModelId" not-null="true" />
        *<one-to-many class="Folder" />*
    </bag>

    <many-to-one update="false" not-null="true" name="*Model*" 
cascade="none" *fetch=select and lazy= false* >
      <column name="ModelId" />
    </many-to-one>


Now If I try to load a Folder by its Id using *session.Get<Folder>(folderId)
*. I see that it is firing a select SQL for iteself.
Then it is firing a select for the associated Model. Then from Model again 
queries are being fired for all child 
objects, subObjects all along the object graph from Model. I am afraid that 
this is going to be real slow if every time so many SQLs
get fired. Is there anything I could do. Or am I doing anything wrong over 
here? 

Any kind of help will be appreciated.

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/nhusers/-/wKKE-Z5Jh0gJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to