title: Insert new NHibernate entity and immediately assign to parent
body: <p>I have parent and child objects BOOKLET and DEMOGRAPHICS_INFO in my Oracle database mapped as follows in my data layer:</p>
  
  <pre><code>public BookletMapping()
  {
      Table("BOOKLET");
      Id(x =&gt; x.Id, m =&gt; m.Column("ID");
      ...
      ManyToOne(x =&gt; x.DemographicsInfo, m =&gt; m.Column("DEMOGRAPHICS_INFO_ID"));
  }
  
  public DemographicsInfoMapping()
  {
      Table("DEMOGRAPHICS_INFO");
      Id(x =&gt; x.Id, m =&gt; m.Column("ID");
      ...
  }
  </code></pre>
  
  <p>I have intentionally left out the DemographicsInfo relationship to the Booklet because I don't need to traverse my entities that direction. The ManyToOne relationship will actually be a one-to-one.</p>
  
  <p>I have written a test to ensure I can create a DemographicsInfo and immediately assign it to its parent Booklet, and that looks like this:</p>
  
  <pre><code>[Test]
  public void ShouldSaveCorrectEntity()
  {
      var booklet = _unitOfWork.Get&lt;Booklet&gt;(4);
      var demInfo = new DemographicsInfo();
      _unitOfWork.Insert(demInfo);
      booklet.DemographicsInfo = demInfo;
      _unitOfWork.Save();
  
      demInfo.Id.ShouldNotEqual(0);
  }
  </code></pre>
  
  <p>When I call Save(), I get the following exception:</p>
  
  <pre><code>{"ORA-02291: integrity constraint (&lt;schema&gt;.BOOKLET_DEMOGRAPHICS_INFO_FK1) violated - parent key not found\n"}
  </code></pre>
  
  <p>This is because the demInfo object is not given an Id upon Insert(). My Insert implementation looks like this:</p>
  
  <pre><code>public void Insert&lt;T&gt;(T entity)
  {
      using (var transaction = _session.BeginTransaction())
      {
          _session.Save(objectToSave);
          _session.Flush();
          transaction.Commit();
      }
  }
  </code></pre>
  
  <p>Where _session is an NHibernate ISession. Because I have both Saved the new entity (it persists successfully) and Flushed my session, I would expect my demInfo variable to have an Id, but it remains 0, which is a foreign key violation when I try to save my parent object. Am I overlooking a step here? Should I rethink my pattern for adding a new child to an existing parent?</p>