I get the following error when attempting to use NHibernate to persist a new instance of 2 objects that have more than one association between them:
NHibernate.Exceptions.GenericADOException: could not insert: [MyProject.BusinessEntities.Client][SQL: INSERT INTO dbo.Client (Version, CurrentEvent_ClientEvent_Id, EntityType, Id) VALUES (?, ?, 'MyProject.BusinessEntities.Client', ?)] ---> System.Data.SqlClient.SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK328F067A46E318FD". The conflict occurred in database "DatabaseName", table "dbo.ClientEvent", column 'ClientId'.
The scenario is as follows:
I have 2 classes (which I have renamed for clarity): Client and ClientEvent. Client has a property called Events which is of type List. ClientEvent contains a property "Client" which is the reverse association back to the Client object. In addition, the Client class has another property called CurrentEvent, which contains a reference to one of the items in the ClientEvents collection.
Client.hbm.xml contains:
<bag name="ClientEvents" access="field.camelcase-underscore" inverse="true" cascade="save-update">
<key column="Client_Id"/>
<one-to-many class="MyProject.BusinessEntities.ClientEvent, MyProject.BusinessEntities" />
</bag>
<many-to-one name="CurrentEvent" access="property" class="MyProject.BusinessEntities.ClientEvent, MyProject.BusinessEntities" column="CurrentEvent_ClientEvent_Id" cascade="save-update" />
ClientStatusRecord.hbm.xml contains:
<many-to-one name="Client" access="property" class="MyProject.BusinessEntities.Client, MyProject.BusinessEntities" column="Client_Id" cascade="save-update" />
I auto-generate the database schema from the mappings, and the foreign keys in both tables are both nullable.
I use the following code to persist a new Client and ClientRecord (again simplified to remove all the session management gumpf)
var newClient = CreateNewClient();
var newEvent = CreateNewEvent();
newEvent.client = newClient; // ensure the reverse association is set
newClient.Events.Add(newEvent);
session.SaveOrUpdate(newClient);
session.Flush();
Having a look at the SQL output from NHibernate, it appears to be attempting to save the Client record before saving the ClientEvent record, attempting to insert the ClientEvent Id into the foreign key which fails because the ClientEvent record does not exist yet
Questions
- Has anyone got it to work where nhibernate persists object with multiple relationships?
- If so, what should the mappings look like?
- Any suggestions for things I can try?
Sorry for the longwinded post