【问题标题】:Coldfusion ORM: delete in cascadeColdfusion ORM:级联删除
【发布时间】:2011-09-04 22:17:41
【问题描述】:

我不是冷融合专家,我打电话给你是因为我在扯头发!

我要删除具有 2 个一对多关系的实体“操作”,即“文本”和“奖励”。

当我尝试删除只有文本但没有奖励的动作时,一切都很好。 Hibernate 删除动作记录和子文本。这就是我想要的!

但是当动作同时有文本和奖励时,我得到了这个错误:

Column 'bonus_actionId' cannot be null
Root cause :java.sql.BatchUpdateException: Column 'bonus_actionId' cannot be null

为什么 Hibernate 在删除 Action 之前不删除 Bonus ?就像文本一样?

谢谢

动作实体:

component {
    property name="id"      column="action_id" type="numeric" fieldtype="id" generator="native";

    /* ... */

    property name="texts" type="array" 
             fieldtype="one-to-many" cfc="Text" fkcolumn="text_actionId" singularname="text"
             cascade="all-delete-orphan" lazy="true";

    /* ... */

    property name="bonus" type="array" 
             fieldtype="one-to-many" cfc="Bonus" fkcolumn="bonus_actionId" singularname="bonus" 
             cascade="all-delete-orphan" lazy="true";
}

文本实体:

component {
    property name="id"      column="text_id" type="numeric" fieldtype="id" generator="native";

    /* ... (properties without relationships */

    property name="action" fieldtype="many-to-one" fkcolumn="text_actionId" cfc="Action" notnull="false" lazy="true";
}

奖励实体:

component  {
    property name="id"      column="bonus_id" type="numeric" fieldtype="id" generator="native";

    /* ... (properties WITH relationships */

    // Parent
    property name="action" fieldtype="many-to-one" fkcolumn="bonus_actionId" cfc="Action" notnull="true" lazy="true";

}

【问题讨论】:

    标签: hibernate orm coldfusion


    【解决方案1】:

    不知何故,Hiberate 会首先将实体设置为 Null(成为孤儿),然后删除孤儿。

    所以.. 从 Bonus.cfc 中的 action 属性中删除 notnull="true" 就可以了。

    【讨论】:

    • 有可能保持 notnull="true" 仍然享受自动抑制吗?没有动作就不可能存在奖金,我发现以前的行为很有用。我知道我可以使用 ORM 事件来处理 Action preDelete 并手动删除所有子项,但就像它是“自然的”一样,我认为它是在 Hibernate 中实现的。是吗?
    【解决方案2】:

    您可以通过将inverse="true" 添加到关系的外键拥有方来保持您的notnull="true" 并让级联正常工作。

    在您的情况下,这将在 Action 实体上:

    component {
    
        property name="id"
                 column="action_id"
                 type="numeric"
                 fieldtype="id"
                 generator="native";
    
        /* ... */
    
        property name="texts"
                 type="array" 
                 fieldtype="one-to-many"
                 cfc="Text"
                 fkcolumn="text_actionId"
                 singularname="text"
                 cascade="all-delete-orphan"
                 inverse="true"
                 lazy="true";
    
        /* ... */
    
        property name="bonus"
                 type="array" 
                 fieldtype="one-to-many"
                 cfc="Bonus"
                 fkcolumn="bonus_actionId"
                 singularname="bonus" 
                 cascade="all-delete-orphan"
                 inverse="true"
                 lazy="true";
    }
    

    Here's a write-up on how inverse works in Hibernate.

    【讨论】:

      猜你喜欢
      • 2014-10-21
      • 2011-11-15
      • 2018-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-24
      • 2011-06-29
      • 2010-10-05
      相关资源
      最近更新 更多