title: NHibernate SaveOrUpdate Child Collection Not Updated With Identity Ids
body: <p>I am clearly missing something (hopefully obvious), and I have had no luck with Google so far.</p>
  
  <p>I have a Parent-Child relationship mapped as follows</p>
  
  <p><strong>Simplified Parent Map</strong></p>
  
  <pre><code>  public sealed class ParentMap : ClassMap&lt;ParentEntity&gt;
    {
      public ParentMap()
      {
        Table("Parent");
        Component(x =&gt; x.Thumbprint);
        Id(x =&gt; x.Id).GeneratedBy.Identity();
  
        Map(x =&gt; x.ServeralNotNullableProperties).Not.Nullable();
  
        HasMany(x =&gt; x.Children).KeyColumn("ChildId")
                                .Inverse()
                                .LazyLoad()
                                .Cascade
                                .AllDeleteOrphan();
  
        References(x =&gt; x.SomeUnrelatedLookupColumn).Column("LookupColumnId").Not.Nullable();
      }
    }
  </code></pre>
  
  <p><strong>Simplified Child Map</strong></p>
  
  <pre><code>  public sealed class ChildMap : ClassMap&lt;ChildEntity&gt;
    {
      public ChildMap()
      {
        Table("Child");
        Component(x =&gt; x.Thumbprint);
        Id(x =&gt; x.Id).GeneratedBy.Identity();
  
        Map(x =&gt; x.MoreNotNullableProperties).Not.Nullable();
  
        References(x =&gt; x.Parent).Column("ParentId").Not.Nullable();
      }
    }
  </code></pre>
  
  <p><strong>Simplified Service Method Steps</strong></p>
  
  <p>I then have a service method that retrieves <em>Parent</em> and adds a new <em>Child</em> via some domain method. The underlying NHibernate code boils down to:</p>
  
  <p>1) Session Opened on WCF AfterReceiveRequest (IDispatchMessageInspector)</p>
  
  <pre><code>_sessionFactory.OpenSession();
  </code></pre>
  
  <p>2) Retrieve existing instance of parent via .Query</p>
  
  <pre><code>_session.Query&lt;ParentEntity&gt;()
          .Where(item =&gt; item.Id == parentId)
          .Fetch(item =&gt; item.SomeLookupColumn)
          .First();
  </code></pre>
  
  <p>3) Add new 'Child' entity to 'Parent' via domain object method such as...</p>
  
  <pre><code>public ChildEntity CreateChild()
  {
    var child = new ChildEntity{ Parent = this };
  
    Children.Add(child);
  
    return child;
  }
  </code></pre>
  
  <p>4) Ultimately calls SaveOrUpdate on 'Parent' entity.</p>
  
  <pre><code> // ChildEntity Id is 0
   _session.SaveOrUpdate(parentEntity)
   // Expect ChildEntity Id to NOT be 0
  </code></pre>
  
  <p>Note: Use of SaveOrUpdate <em>Does</em> persist changes to database; use of Merge results in TransientObjectException mentioned below.</p>
  
  <p>5) Finally, transaction committed on WCF BeforeSendReply (IDispatchMessageInspector)</p>
  
  <pre><code> _session.Commit();
  </code></pre>
  
  <p><strong>The Problem</strong></p>
  
  <p>Typically when an entity is saved, after the call to SaveOrUpdate, the Id of said entity refects the Identity generated by the INSERT statement. However, when I add a new child entity to an existing parent, the associated entity in <code>Children</code> does not get assigned an Id. I need to pass this Id back to the caller so subsequent calls result in UPDATES and not additional INSERTS.</p>
  
  <p>Why is NHibernate not updating the underlying entity? 
  Do I explicitly have to call SaveOrUpdate on the child (that sucks if that is the case)?</p>
  
  <p>Any thoughts on the matter greatly appreciated. Thanks!</p>
  
  <p><strong>EDIT</strong>
  I should note, that the new child entity IS being saved as expected; I just don't get back the Id assigned.</p>
  
  <p><strong>UPDATE</strong>
  Tried switching to .Merge(~) as suggested by Michael Buen below; resulting in a TransientObjectException telling me to explicitly save my modified child entity before calling merge. I also experimented with .Persist(~) to no avail. Any additional insight greatly appreciated.</p>
  
  <blockquote>
    <p>object is an unsaved transient instance - save the transient instance before merging: My.NameSpace.ChildEntity</p>
  </blockquote>