0

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]

Steven
  • 860
  • 6
  • 24
  • Can you include the Sql nHibernate's running, your save code and the error you're getting? – Kai G Feb 07 '12 at 03:24

1 Answers1

0

You need to configure the cascade for those references to Save-Update. This means, when you save or update an entity, it will save the references "by cascade".

http://wiki.fluentnhibernate.org/Getting_started

In the Mappings section there's an example with cascade.

Ivo
  • 8,172
  • 5
  • 27
  • 42
  • both references are set to SaveUpdate. I believe the problem is because I'm also adding it to the collection. – Steven Feb 07 '12 at 12:31