Hibernate version: 2.0.1GA
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.
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).
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:
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();
Note that ADDRESS is related to CONTACT. 1 CONTACT per MANY ADDRESSES.
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.
Better way out there?
Thanks!