title: NHibernate: Persist an object with children
body: <p>I am trying to persist an object with a collection of child objects. I can't persist the children first as there is a FK relationship. I could save the parent first and then add the children on to it, but this would introduce more work. Basically I'm just trying to save a fully populated object in one step and not break it into parts. Is there something wrong with my mapping (sorry it looks so ugly) or is it my methods?</p>
  
  <p>Parent:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"&gt;
    &lt;class name="NetworkOrderManagement.Core.Order, NetworkOrderManagement.Core" table="NETORDMGMT.ORDERHEADER" lazy="false" &gt;
      &lt;id name="OrderId" column="ORDERID" type="int"&gt;
        &lt;generator class="seqhilo"&gt;
          &lt;param name="sequence"&gt;ORDERID_SEQ&lt;/param&gt;
        &lt;/generator&gt;
      &lt;/id&gt;
      &lt;property name="TransmissionDate" column="TRANSMISSIONDATE" type="DateTime"/&gt;
      &lt;property name="StoreNumber" column="STORENUMBER" type="Int16"/&gt;
      &lt;property name="Department" column="DEPARTMENT" type="Int16"/&gt;
      &lt;property name="OrderType" column="ORDERTYPE" type="Int16"/&gt;
      &lt;property name="OrderSequence" column="ORDERSEQUENCE" type="Int16"/&gt;
      &lt;property name="ExtractTime" column="EXTRACTTIME" type="DateTime"/&gt;
      &lt;property name="Status" column="STATUS" type="Int16"/&gt;
      &lt;property name="ReceivedTime" column="RECEIVEDTIME" type="DateTime"/&gt;
      &lt;bag name="OrderDetail" table="NETORDMGMT.ORDERDETAIL" 
           lazy="false" cascade="all" inverse="true"&gt;
        &lt;key column="ORDERID" on-delete="cascade"/&gt;
        &lt;one-to-many class="NetworkOrderManagement.Core.OrderDetail, NetworkOrderManagement.Core"  /&gt;
      &lt;/bag&gt;
    &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>Child:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"&gt;
    &lt;class name="NetworkOrderManagement.Core.OrderDetail, NetworkOrderManagement.Core" table="NETORDMGMT.ORDERDETAIL" lazy="false"&gt;
      &lt;id name="OrderDetailId" column="ORDERDETAILID" type="int"&gt;
        &lt;generator class="seqhilo"&gt;
          &lt;param name="sequence"&gt;"ORDERDTLID_SEQ"&lt;/param&gt;
        &lt;/generator&gt;
      &lt;/id&gt;
  &lt;many-to-one name="Order" class="NetworkOrderManagement.Core.Order, NetworkOrderManagement.Core"
                   column="OrderId" lazy="false" not-null="true" /&gt;
      &lt;property name="ItemNumber" column="ITEMNUMBER" type="Int32"/&gt;
      &lt;property name="OrderQuantity" column="ORDERQUANTITY" type="Int32"/&gt;
      &lt;property name="ErrorCode" column="ERRORCODE" type="Int32"/&gt;
    &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>Here's my exception:</p>
  
  <pre><code>Test method NetworkOrderManagement.Tests.DataAccess.QuickTests.QuickTest threw exception:
  Distribution.Exceptions.DataAccessException: NHibernate Exception ---&gt;
  NHibernate.PropertyValueException: not-null property references a null or transient valueNetworkOrderManagement.Core.OrderDetail.Order.
  </code></pre>
  
  <p>I get this when my test below tries to add an orderdetail to the order while it is still transient:</p>
  
  <pre><code>    [TestMethod]
      public void QuickTest()
      {
          myOrderRepository = NetworkOrderManagement.Data.RepositoryFactory.Instance.GetOrderRepository();
          myOrderDetailRepository = NetworkOrderManagement.Data.RepositoryFactory.Instance.GetOrderDetailRepository();
          myOrder = new Order { StoreNumber = RandGen.LittleRand(), Department = RandGen.LittleRand(), TransmissionDate = DateTime.MinValue, ExtractTime = DateTime.MinValue, ReceivedTime = DateTime.MinValue };
          myOrder = myOrderRepository.Save(myOrder);
  
          myOrderDetail1 = new OrderDetail {OrderId = myOrder.OrderId, ItemNumber = RandGen.BigRand(), OrderQuantity = RandGen.LittleRand() };
          myOrderDetail2 = new OrderDetail {OrderId = myOrder.OrderId, ItemNumber = RandGen.BigRand(), OrderQuantity = RandGen.LittleRand() };
          myOrderDetail1 = myOrderDetailRepository.Save(myOrderDetail1);
          myOrderDetail2 = myOrderDetailRepository.Save(myOrderDetail2);
          myOrder.OrderDetail.Add(myOrderDetail1);
          myOrder.OrderDetail.Add(myOrderDetail2);
  
          myOrderRepository.CommitChanges();
  
          myOrderDetailRepository.Delete(myOrderDetail2);
          myOrderRepository.CommitChanges();
          myOrderRepository.Delete(myOrder);
          myOrderRepository.CommitChanges();
      }
  </code></pre>