Note that there are some explanatory texts on larger screens.

plurals
  1. PONHibernate one-to-many relationship lazy loading when already loaded
    text
    copied!<p>I have a tree where every node is a Resource class: </p> <pre><code>public abstract class Resource { public virtual Guid Id { get; set; } public virtual Resource Parent { get; set; } public virtual IList&lt;Resource&gt; ChildResources { get; set; } } </code></pre> <p>as you can see this class is abstract and there are many different derived classes from Resource (3 at the moment, more to come). </p> <p>In my database i have a table for Resource, and a table for each class which derives from Resource. These are mapped together with <code>&lt;joined-subclass&gt;</code>. </p> <p>I've read this: </p> <p><a href="http://ayende.com/Blog/archive/2009/08/28/nhibernate-tips-amp-tricks-efficiently-selecting-a-tree.aspx" rel="nofollow noreferrer">http://ayende.com/Blog/archive/2009/08/28/nhibernate-tips-amp-tricks-efficiently-selecting-a-tree.aspx</a></p> <p>and i have the same code as Ayende to load my tree:</p> <pre><code>var resource = UnitOfWork.Current.Session .CreateQuery("from Resource r join fetch r.ChildResources") .SetResultTransformer(new DistinctRootEntityResultTransformer()) .SetReadOnly(true) .List&lt;Resource&gt;(); </code></pre> <p>which is all working fine (all Resources are returned with a single select) However, I'm seeing extra selects occurring as I enumerate a Resource's ChildResources list. </p> <p>Is that because of this?: </p> <p><a href="http://ayende.com/Blog/archive/2009/09/03/answer-the-lazy-loaded-inheritance-many-to-one-association-orm.aspx" rel="nofollow noreferrer">http://ayende.com/Blog/archive/2009/09/03/answer-the-lazy-loaded-inheritance-many-to-one-association-orm.aspx</a></p> <p>Either way, how do I prevent this from happening? </p> <p>Here's the part of the mappings for the relationships (class names trimmed for clarity): </p> <pre><code>&lt;bag cascade="save-update" fetch="join" lazy="false" inverse="true" name="ChildResources"&gt; &lt;key&gt; &lt;column name="Parent_Id" /&gt; &lt;/key&gt; &lt;one-to-many class="Resource" /&gt; &lt;/bag&gt; &lt;many-to-one class="Resource" name="Parent"&gt; &lt;column name="Parent_Id" /&gt; &lt;/many-to-one&gt; </code></pre> <p>Thanks</p> <p><b>UPDATE</b></p> <p>Slight oversight, its only issuing extra selects when enumerating the child collections of the leaf nodes in the tree...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload