zoukankan      html  css  js  c++  java
  • NHibernate Cascades: the different between all, alldeleteorphans and saveupdate

    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.

  • 相关阅读:
    带你玩转Flink流批一体分布式实时处理引擎
    都2022年了,你的前端工具集应该有vueuse
    云图说|图解DGC:基于华为智能数据湖解决方案的一体化数据治理平台
    面试官: Flink双流JOIN了解吗? 简单说说其实现原理
    4种Spring Boot 实现通用 Auth 认证方式
    这8个JS 新功能,你应该去尝试一下
    Scrum Master需要具备哪些能力和经验
    dart系列之:时间你慢点走,我要在dart中抓住你
    dart系列之:数学什么的就是小意思,看我dart如何玩转它
    dart系列之:还在为编码解码而烦恼吗?用dart试试
  • 原文地址:https://www.cnblogs.com/kedach/p/1285097.html
Copyright © 2011-2022 走看看