title: NHibernate Error - Save the Transient Instance Before Flushing
body: <p>I had successfully written a system that contained an order with child order lines, using cascade updates from the order to save the order lines.  In the next iteration I want to associate the order lines with a despatch class.  This seemed quite straight forward - add a nullable DespatchID column to the order line table as a foriegn key to the Despatch table.  However, when I add this to the mapping and save the order object with the despatches not set on the order lines, I get the error "object references an unsaved transient instance - save the transient instance before flushing".</p>
  
  <p>If I remove the association between the order line and the despatch, it saves OK.  I can only presume that the error is caused because it is trying to save the despatch, which does not exist.</p>
  
  <p>Here are the mappings (the order lines class is called OrderProductAmount):</p>
  
  <pre><code>  &lt;class name="NHS.WebTeam.PandemicFluDistribution.Order, NHS.WebTeam.PandemicFluDistribution" table="[Order]" lazy="false" optimistic-lock="version" where="Deleted=0" &gt;
      &lt;id name="ID" type="Int32" column="OrderID" unsaved-value="0"&gt;
        &lt;generator class="hilo"&gt;
          &lt;param name="table"&gt;NHibernateHiLo&lt;/param&gt;
          &lt;param name="column"&gt;NextValue&lt;/param&gt;
          &lt;param name="max_lo"&gt;100&lt;/param&gt;
        &lt;/generator&gt;
      &lt;/id&gt;
  
      &lt;version column="version" name="Version"/&gt;
      &lt;property name="Deleted" column="Deleted" /&gt;
  
      &lt;property name="DateEntered"&gt;&lt;/property&gt;
      &lt;property name="RequiredDeliveryDate"&gt;&lt;/property&gt;
  
      &lt;many-to-one name="Practice" column="PracticeID"&gt;&lt;/many-to-one&gt;
  
      &lt;set name="OrderProductAmounts" access="field.camelcase-underscore" inverse="true" cascade="all-delete-orphan" lazy="true"&gt;
        &lt;key column="OrderID"&gt;&lt;/key&gt;
        &lt;one-to-many class="NHS.WebTeam.PandemicFluDistribution.OrderProductAmount, PandemicFluDistribution" /&gt;
      &lt;/set&gt;
  
    &lt;/class&gt;
  
    &lt;class name="NHS.WebTeam.PandemicFluDistribution.OrderProductAmount, NHS.WebTeam.PandemicFluDistribution" table="OrderProductAmount" lazy="false" optimistic-lock="version" where="Deleted=0" &gt;
      &lt;id name="ID" type="Int32" column="OrderProductAmountID" unsaved-value="0"&gt;
        &lt;generator class="hilo"&gt;
          &lt;param name="table"&gt;NHibernateHiLo&lt;/param&gt;
          &lt;param name="column"&gt;NextValue&lt;/param&gt;
          &lt;param name="max_lo"&gt;100&lt;/param&gt;
        &lt;/generator&gt;
      &lt;/id&gt;
  
      &lt;version column="version" name="Version"/&gt;
      &lt;property name="Deleted" column="Deleted" /&gt;
  
      &lt;many-to-one name="Order" column="OrderID"&gt;&lt;/many-to-one&gt;
      &lt;many-to-one name="ProductAmount" column="ProductAmountID"&gt;&lt;/many-to-one&gt;
      &lt;many-to-one name="Despatch" column="DespatchID" cascade="none" not-null="false"&gt;&lt;/many-to-one&gt;
    &lt;/class&gt;
  
    &lt;class name="NHS.WebTeam.PandemicFluDistribution.Despatch, NHS.WebTeam.PandemicFluDistribution" table="Despatch" lazy="false" optimistic-lock="version" where="Deleted=0" &gt;
      &lt;id name="ID" type="Int32" column="DespatchID" unsaved-value="0"&gt;
        &lt;generator class="hilo"&gt;
          &lt;param name="table"&gt;NHibernateHiLo&lt;/param&gt;
          &lt;param name="column"&gt;NextValue&lt;/param&gt;
          &lt;param name="max_lo"&gt;100&lt;/param&gt;
        &lt;/generator&gt;
      &lt;/id&gt;
  
      &lt;version column="version" name="Version"/&gt;
      &lt;property name="Deleted" column="Deleted" /&gt;
  
      &lt;property name="DateDespatched"&gt;&lt;/property&gt;
      &lt;property name="RequiredDeliveryDate"&gt;&lt;/property&gt;
  
      &lt;many-to-one name="Practice" column="PracticeID"&gt;&lt;/many-to-one&gt;
  
      &lt;set name="OrderProductAmounts" access="field.camelcase-underscore" inverse="true" cascade="none" lazy="true"&gt;
        &lt;key column="DespatchID"&gt;&lt;/key&gt;
        &lt;one-to-many class="NHS.WebTeam.PandemicFluDistribution.OrderProductAmount, PandemicFluDistribution" /&gt;
      &lt;/set&gt;
  
    &lt;/class&gt;
  </code></pre>
  
  <p>The code to create the order is essentially this:</p>
  
  <pre><code>Dim practice = ... get relevant Practice ...
  Dim productAmount = ... get relevant ProductAmount ...
  Dim newOrder as Order = new Order(practice)
  newOrder.AddProductAmount(new OrderProductAmount(newOrder, productAmount)
  OrderDAO.Save(newOrder)
  </code></pre>
  
  <p>Does anybody have any ideas?</p>