title: NHibernate: How is identity Id updated when saving a transient instance?
body: <p>If I use session-per-transaction and call:</p>
  
  <p><strike><strong>session.SaveOrUpdate(entity)</strong></strike>    <em>corrected:</em><br>
  <strong>session.SaveOrUpdateCopy(entity)</strong>   </p>
  
  <p>..and entity is a transient instance with identity-Id=0. Shall the above line automatically update the Id of the entity, and make the instance persistent? Or should it do so on transaction.Commit? Or do I have to somehow code that explicitly?</p>
  
  <p>Obviously the Id of the database row (new, since transient) is autogenerated and saved as some number, but I'm talking about the actual parameter instance here. Which is the business logic instance. </p>
  
  <hr>
  
  <p><strong>EDIT</strong> - Follow-up, of related problem. </p>
  
  <p><strong>Mappings:</strong></p>
  
  <pre><code>public class StoreMap : ClassMap&lt;Store&gt;
  {
      public StoreMap()
      {
          Id(x =&gt; x.Id).GeneratedBy.Identity();
          Map(x =&gt;  x.Name);
          HasMany(x =&gt; x.Staff)    // 1:m
              .Cascade.All();       
          HasManyToMany(x =&gt; x.Products)  // m:m
              .Cascade.All()
              .Table("StoreProduct");    
      }
  }
  
  public class EmployeeMap : ClassMap&lt;Employee&gt; 
  {
      public EmployeeMap()
      {
          Id(x =&gt; x.Id).GeneratedBy.Identity();   
          Map(x =&gt; x.FirstName);
          Map(x =&gt; x.LastName);
          References(x =&gt; x.Store);    // m:1
      }
  }
  
  public class ProductMap : ClassMap&lt;Product&gt;
  {
      public ProductMap() 
      {
          Id(x =&gt; x.Id).GeneratedBy.Identity();
          Map(x =&gt; x.Name).Length(20);
          Map(x =&gt; x.Price).CustomSqlType("decimal").Precision(9).Scale(2);
          HasManyToMany(x =&gt; x.StoresStockedIn)
          .Cascade.All()
          .Inverse()
          .Table("StoreProduct");
       } 
  }
  </code></pre>
  
  <hr>
  
  <p><strong>EDIT2</strong></p>
  
  <p><strong>Class definitions:</strong></p>
  
  <pre><code>   public class Store
  {
      public int Id { get; private set; }
      public string Name { get; set; }
      public IList&lt;Product&gt; Products { get; set; }
      public IList&lt;Employee&gt; Staff { get; set; }
  
      public Store()
      {
          Products = new List&lt;Product&gt;();
          Staff = new List&lt;Employee&gt;();
      }
  
  
      // AddProduct &amp; AddEmployee is required. "NH needs you to set both sides before
      // it will save correctly" 
  
      public void AddProduct(Product product)
      {
          product.StoresStockedIn.Add(this);
          Products.Add(product);
      }
  
      public void AddEmployee(Employee employee)
      {
          employee.Store = this;
          Staff.Add(employee);
      }
  }
  
  public class Employee
  {
      public int Id { get;  private set; }
      public string FirstName { get;  set; }
      public string LastName { get;  set; }
      public Store Store { get; set; }
  }
  
  public class Product
  {
      public int Id { get; private set; }
      public string Name { get; set; }
      public decimal Price { get; set; }
      public IList&lt;Store&gt; StoresStockedIn { get; private set; }
  }
  </code></pre>