title: Saving multiple child entities in NHibernate, at once
body: <p>I'm getting the "Save unsaved transient entities" error in NHibernate. I have an aggregate root, <code>neighborhood</code> that contains <code>addresses</code> and <code>person</code>, here's some quick pseudo code to explain the relationship:</p>
  
  <pre><code>public class Neighborhood {
     public virtual int Id { get; set; }
     public virtual IList&lt;Address&gt; Addresses { get; set; }
  }
  
  public class Address {
     public virtual int Id { get; set; }
     public virtual string Address { get; set; }
     public virtual Person Person { get; set; } //Assume only one person per address
  }
  
  public class Person {
     public virtual int Id { get; set; }
     public virtual string Name { get; set; }
  }
  </code></pre>
  
  <p>In my Neighborhood map I have:</p>
  
  <pre><code>   mapping.HasMany(x =&gt; x.Addresses)
          .Inverse()
          .KeyColumn("NeighborhoodFk")
          .Cascade.All()
          .AsBag();
  </code></pre>
  
  <p>In my code I will often want to create a and associate an Address and Person at the same time:</p>
  
  <pre><code>var address = new Address();
  var person = new Person();
  
  var address.Person = person;
  
  var neighborhood = neighborhoodRepository.Get(id);
  
  neighborhood.Add(address);
  
  neighborhoodRepository.DbContext.BeginTransaction();
  neighborhoodRepository.SaveOrUpdate(neighborhood);
  neighborhoodRepository.DbContext.CommitTransation();
  </code></pre>
  
  <p>I will get the "unsaved transient entities" error on the <code>Person</code> entity because it is attached to the transient entity <code>Address</code>.</p>
  
  <p>The only way I can see around this is to save the <code>address</code> first, make another call to the database to update <code>neighborhood</code> after the update, search for the <code>address</code> I just added, attach the <code>person</code> and then save again.</p>
  
  <p>Is there something I'm missing to make this easier? This seems like a common use case and I don't want to be making a bunch of roundtrips to the database.</p>