I have the following object model (based on a legacy table structure that I can't change).
public class Store
Contact BillingContact {get;set;}
Contact SetupContact {get;set;}
ISet<Contact> Contacts {get;set;}
In this scenario, when I create a new Store, I also add a new contact to the Contacts list. I also want to set that contact to be the reference on the BillingContact and the SetupContact. Then save the lot as a single transaction. If I save the store without setting the Reference to BillingContact and SetupContact, everything is fine. But when I set those properties, NHibernate tries to do an Update after the Insert to update the references and I get an error because the Store hasn't actually been inserted yet.
It looks like I may have to use a trigger or stored proc, but I'm hoping there is an NHibernate way.
The SQL being run is
INSERT INTO CONTACTS (Id, CompanyId, Name, etc.) Values (1234, NULL, "My Contact", etc.) NOTE: 1234 was retrieved from a sequence
Then
INSERT INTO company (Id, BillingContactId, SetupContactId) Values (8946, 1234, 1234)
Then - Error is because of this
UPDATE CONTACTS SET CompanyId=8946 WHERE CUSTOMER_ID = 1234 AND TimeStamp = xxx
Since the insert has not been committed the final statement fails.
The error is Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) [Customer#1234]