Note that there are some explanatory texts on larger screens.

plurals
  1. POSimple NHibernate Eager Loading confusion
    primarykey
    data
    text
    <p>I have a very simple master-child model: </p> <p><strong>WorkflowEscalationPolicy&lt;--WorkflowEscalation</strong>. </p> <p>The WorkflowEscalations are accessible from the <code>WorkflowEscalationPolicy.Escalations</code> property.</p> <p>I want to eagerly fetch the escalations when I get the <code>WorkflowEscalationPolicy</code> items.</p> <p>I have a method that gets all escalation policies. I have tried using this:</p> <pre><code>IEnumerable&lt;IWorkflowEscalationPolicy&gt; results = Session .Query&lt;WorkflowEscalationPolicy&gt;() .Fetch(x=&gt;x.Escalations) .Where(q=&gt;q.ApplicationKey==applicationKey); </code></pre> <p>and this:</p> <pre><code>IEnumerable&lt;IWorkflowEscalationPolicy&gt; results = Session .CreateCriteria&lt;WorkflowEscalationPolicy&gt;() .SetFetchMode("Escalations", FetchMode.Eager) .List&lt;WorkflowEscalationPolicy&gt;(); </code></pre> <p>to accomplish this, but neither work - in that the child Escalations collection remains empty. I know that there are <code>WorkflowEscalations</code> in the database.</p> <p>My mapping files:</p> <pre><code>&lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="FB.SimpleWorkflow.NHibernate" namespace="FB.SimpleWorkflow.NHibernate.Model" &gt; &lt;class name="WorkflowEscalationPolicy" table="SW_WorkflowEscalationPolicy" &gt; &lt;id name="Id"&gt; &lt;generator class="native" /&gt; &lt;/id&gt; &lt;property name="ApplicationKey" /&gt; &lt;property name="ActivityKey" /&gt; &lt;property name="InstanceKey" /&gt; &lt;property name="State" /&gt; &lt;property name="EscalateAfterSeconds" /&gt; &lt;property name="MaximumEscalationsPerTransition" /&gt; &lt;set name="Escalations" table="SW_WorkflowEscalation" cascade="all-delete-orphan" inverse="true"&gt; &lt;key column="WorkflowEscalationPolicyId" /&gt; &lt;one-to-many class="WorkflowEscalation" /&gt; &lt;/set&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="FB.SimpleWorkflow.NHibernate" namespace="FB.SimpleWorkflow.NHibernate.Model" &gt; &lt;class name="WorkflowEscalation" table="SW_WorkflowEscalation" &gt; &lt;id name="Id"&gt; &lt;generator class="native" /&gt; &lt;/id&gt; &lt;property name="EscalatedTimeStamp" /&gt; &lt;property name="EscalatedByUserName" /&gt; &lt;property name="EscalatedOnHost" /&gt; &lt;many-to-one name="WorkflowEscalationPolicy" column="WorkflowEscalationPolicyID" /&gt; &lt;many-to-one name="WorkflowActivityInstanceTransition" column="WorkflowActivityInstanceTransitionId" /&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p>What is the difference between the two and what am I missing here? I thought that in the first instance NHibernate refers to the mapping files for loading strategies?</p> <p>NHibernate is v3.1</p> <h2><strong>Update: 6 June 2013</strong></h2> <p>Further investigations thanks to @cremor ...</p> <p>I've profiled the generated SQL that the query executes and I get:</p> <pre><code>SELECT this_.Id as Id4_1_, this_.ApplicationKey as Applicat2_4_1_, this_.ActivityKey as Activity3_4_1_, this_.InstanceKey as Instance4_4_1_, this_.State as State4_1_, this_.EscalateAfterSeconds as Escalate6_4_1_, this_.MaximumEscalationsPerTransition as MaximumE7_4_1_, escalation2_.WorkflowEscalationPolicyID as Workflow5_3_, escalation2_.Id as Id3_, escalation2_.Id as Id2_0_, escalation2_.EscalatedTimeStamp as Escalate2_2_0_, escalation2_.EscalatedByUserName as Escalate3_2_0_, escalation2_.EscalatedOnHost as Escalate4_2_0_, escalation2_.WorkflowEscalationPolicyID as Workflow5_2_0_, escalation2_.WorkflowActivityInstanceTransitionID as Workflow6_2_0_ FROM SW_WorkflowEscalationPolicy this_ left outer join SW_WorkflowEscalation escalation2_ on this_.Id=escalation2_.WorkflowEscalationPolicyID </code></pre> <p>Which is what I would expect.</p> <p>This generates the correct SQL, with the single detail Escalation record to the right of the master EscalationPolicy record. Therefore, the database is fine, the T-SQL is fine and therefore there is a misunderstanding within the NHibernate somewhere.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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