I tried searching this but did not get an answer and hence posting it. If this is repeat question, my apologies.
I have a one to many relationship between two classes. Order class has a IList<OrderLine> and in the database there are two tables Order and OrderLine corresponding to these classes. Order object is created by NHibernate along with the list of order lines populated (lazy = false). Let's say there are three order lines #1, #2 and #3, to start with. Now, line #4 is added and #1 got dropped. So, I call clear() of IList<OrderLine> and insert the lines that are relevant now back into IList, after the change, which are #2, #3 and #4. Then I call the session.Update() for the order object. My expectation is it will delete the record in OrderLine table for #1 and insert #4.
In the order.hbm.xml, if I set cascade="all-delete-orphan" and inverse="false" (or just leave the inverse attribute out), it tries to update the Order ID of all the OrderLine records as null. Since database will not allow this (NOT NULL column), it fails. Whatever I do, I couldn't make it issue DELETEs. Of course by setting, inverse="true" in Order.hbm.xml and then calling individual deletes on OrderLine (session.Delete(orderLine)), I can accomplish what I want.
But I'm 100% sure, if every thing done correctly, NHibernate itself will do the deletion of records in OrderLine. But I couldn't get it to work. In the Order.hbm.xml, I have cascade set to all-delete-orphan and inverse="false". I believe only these two settings will do the trick but it is not to be. Am I wrong with the way I'm clearing the collection and creating the orderline objects and adding them back? Any pointers from the experts will be appreciated.
|