繁体   English   中英

Coldfusion ORM:级联删除

[英]Coldfusion ORM: delete in cascade

我不是 coldfusion orm 方面的专家,我打电话给你,因为我在扯头发!

除了删除具有 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";

}

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

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

您可以通过将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";
}

这是一篇关于 Hibernate 中的inverse如何工作的文章。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM