I have a class, BillMedicine, which is many-to-many table for Bill and Medicine. The BillMedicine mapping file is:
<class name="BillMedicine" table="Bill_Medicine">
<composite-id>
<key-many-to-one name="Bill" class="Bill" column="BillID" />
<key-many-to-one name="Medicine" class="Medicine" column="MedicineID" />
</composite-id>
<version name="LastModifiedOn" column="LastModifiedOn" type="DateTime"
access="field.pascalcase-underscore" />
<property name="Quantity" column="QuantityPurchased" />
</class>
When I create a new BillMedicine and update the quantity my unit test succeeds. However if I uncomment the line that updates the medicine, I got the exception NHibernate.StaleObjectStateException: Row was updated or deleted by another transaction
BillMedicineRepository target = new BillMedicineRepository();
BillMedicine billMedicine =
new BillMedicine { Bill = _bill, Medicine = _medicine1, Quantity = 1 };
target.AddBillMedicine(billMedicine);
//billMedicine.Medicine = _medicine2;
billMedicine.Quantity = 2;
target.UpdateBillMedicine(billMedicine); // Exception if I update the medicine
I have checked the SQL shown by NHibernate and it is not updating the medicine
UPDATE Bill_Medicine SET LastModifiedOn = @p0, QuantityPurchased = @p1
WHERE BillID = @p2 AND MedicineID = @p3 AND LastModifiedOn = @p4;
Edit:
My Questions:
1- Why updating the quantity only is working but updating the medicine is throwing exception?
2- Is it possible to make Nhibernate update the medicine property? If yes, how? If no, can someone suggest a workaround?