NHibernate Cascades: the different between all, all-delete-orphans and save-update

NHibernate Cascades: the different between all, all-delete-orphans and save-update

I have posted in the past about NHibernate's cascade being one of the places that require careful attention. But I run into an issue with it yesterday.

The issue was that we had a typical parent-children scenario, but the requirement changed and we had to support orphans. That is, children without a parent. This isn't that big of a deal, after all, and I told Imperial to just change the cascade from all-delete-orphans to all, and forgot about it.

I was called a few minutes afterward, and saw that the change didn't have the desired effect. The scenario that we were working on was deleting the parent, where the child needed to remain behind (will null foreign key, of course). Now, I was being stupid, and I started debugging into NHibernate to figure out what was the cause of this "bug". Ten minutes later (took a while to find where exactly this was happening), I had an "Oh, I am so dumb" moment.

So, to save myself from future embarassment, let me try to articulate what it means:

NHibernate Cascades:
Entities has assoications to other objects, this may be an assoication to a single item (many-to-one) or an assoication to a collection (one-to-many, many-to-any).
At any rate, you are able to tell NHibernate to automatically traverse an entity's assoications, and act according to the cascade option. For instnace, adding an unsaved entity to a collection with save-update cascade will cause it to be saved along with its parent object, without any need for explicit instructions on our side.

Here is what each cascade option means:

  • none - do not do any cascades, let the users handles them by themselves.
  • save-update - when the object is saved/updated, check the assoications and save/update any object that require it (including save/update the assoications in many-to-many scenario).
  • delete - when the object is deleted, delete all the objects in the assoication.
  • delete-orphans - when the object is deleted, delete all the objects in the assoication. In addition to that, when an object is removed from the assoication and not assoicated with another object (orphaned), also delete it.
  • all - when an object is save/update/delete, check the assoications and save/update/delete all the objects found.
  • all-delete-orhpans - when an object is save/update/delete, check the assoications and save/update/delete all the objects found. In additional to that, when an object is removed from the assoication and not assoicated with another object (orphaned), also delete it.

Fairly simple, isn't it? I have no idea why I managed to forget this. At any rate, my issue was that I set the cascade to all, and then deleted to root object, which naturally deleted all the child objects. Setting it to save-update was what I wanted to do, once I did that, everything went just fine.

转载于:https://www.cnblogs.com/kedach/archive/2008/09/05/1285097.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值