Sunny
Sunny

Reputation: 4809

How will NHibernate update the parent and child tables

I am working on MVC app with NHibernate. I am having a screen with Customer information and Order objects. As guessed, customer entity is having List property and Order entity is having customer property.

I attached the orders object by storing them in session. User can add/delete the orders, so I have attached modified orders to the customer object. But when I see the sql statements, it is inserting the new orders which is expected behavior but for deleted orders it is updating with customer_id = null instead of deleting them completely?

Thanks, Sundeep

Upvotes: 0

Views: 224

Answers (1)

J. Ed
J. Ed

Reputation: 6742

you're looking for cascade options.
The reason your orders were being erased is that you persisted an empty collection, so nHibernate assumed that the collection was emptied on purpose, and updated that collection items in the Db by removing the associations.

The behavior that you most probably want is AllDeleteOrphan which would delete orders removed from your customer.

Upvotes: 1

Related Questions