title: NHibernate - creating records in related tables
body: <p>Hibernate version: 2.0.1GA </p>
  
  <p>I'm trying to add a single record in two tables. The tables are related. I have a beginner's knowledge of NHiberate so I'm hoping there's a better way to do this. </p>
  
  <p>Presently, I am under the belief that the only way I can get the ID of a newly-added record is to perform a SaveOrUpdate; then the object's ID field gets set (since I'm using autoincrement with SQL Server 2005 Express). </p>
  
  <p>In order to add a record in a related table, I have to set the ID field of its parent. Here's what I'm doing now: </p>
  
  <pre><code>transaction = session.BeginTransaction(); 
  
  Contact myContact = new Contact(); 
  
  myContact.Company = "ABC Company"; 
  myContact.Notes = "test"; 
  
  session.SaveOrUpdate(myContact); 
  
  Address myAddress = new Address(); 
  myAddress.IdContact = myContact.Id; 
  myAddress.City = "Any City"; 
  myAddress.State = "XX"; 
  myAddress.Zip = "12345"; 
  
  session.SaveOrUpdate(myContact); 
  
  // Commit transaction 
  transaction.Commit(); 
  </code></pre>
  
  <p>Note that ADDRESS is related to CONTACT. 1 CONTACT per MANY ADDRESSES. </p>
  
  <p>In order to add the ADDRESS record, I have to have the corresponding CONTACT ID and I believe I can only get that AFTER I create the CONTACT record, hence my use of "SaveOrUpdate" twice. </p>
  
  <p>Better way out there? </p>
  
  <p>Thanks!</p>