I’m using NHibernate in my project. I just thought to get some inputs from you. The problem I’m facing is as below:
I have three DB tables which are related. The relation is as shown below
- Table1 (Field1_PK, Other_fields)
- Table2 (Field1_PK, Field2_FK_To_Table1_PK, Other_Fields)
- Table3 (Field1_PK, Field2_FK_To_Table2_PK, Other_Fields)
Table3-->Table2-->Table1
I have separate NH mapping files for these three entities. The relationships between these tables are mapped in the files using the tags. The cascade="all-delete-orphan" also is specified. Now, I’m trying to move one record in Table3 to another record in Table2 which is not yet exist which in turn need to move to another record in Table1. So, ideally as a single transaction the below steps should happen in the order specified: 1. Delete the record from Table3 2. Delete the old record from Table2 3. Delete old record from Table1 4. Create new record in Table1 5. Create new record in Table2 6. Create new record in Table3
To do this, I have retrieved the Table3 record entity and changed its property to make it associated to the new Table2 record entity.
Unfortunately, I’m getting the unique key violation error which is thrown based on the Table3 unique key. The reason for that is step6 (INSERT) happens first. Since the table already has a record with the same key, it throws the error. I removed the unique key index from the table and found that functionality is working fine, but the same step6 happens first. But altogether the functionality works fine. But, I have to have the unique key index in the table.
Please write your comments if you have any idea on this!
Daniel