简体   繁体   English

Coldfusion ORM:级联删除

[英]Coldfusion ORM: delete in cascade

I'm not an expert in coldfusion orm and I call your help because I'm pulling my hair!我不是 coldfusion orm 方面的专家,我打电话给你,因为我在扯头发!

I'm excepting to delete an entity 'Action' that has 2 relationship one-to-many, 'Texts' and 'Bonus'.除了删除具有 2 个一对多关系“文本”和“奖励”的实体“操作”之外。

When I try to delete an Action that has only Texts but no Bonus, everything is fine.当我尝试删除只有文本但没有奖励的动作时,一切都很好。 Hibernate deletes the Action record and the children Texts. Hibernate 删除操作记录和子文本。 It's what I want!这就是我想要的!

But when the Action has both Texts and Bonus, I got this error:但是当动作同时有文本和奖励时,我得到了这个错误:

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

Why Hibernate does not delete the Bonus before deleting the Action?为什么 Hibernate 在删除 Action 之前不删除 Bonus? Like it is done for Texts?就像文本一样?

Thanks谢谢

Action Entity:动作实体:

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";
}

Text Entity:文本实体:

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";
}

Bonus Entity:奖励实体:

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";

}

Somehow Hiberate would first set the entity to Null (become orphan), then delete the orphans.不知何故,Hiberate 会首先将实体设置为 Null(成为孤儿),然后删除孤儿。

So.. remove notnull="true" from property action in Bonus.cfc and you're all set.所以.. 从 Bonus.cfc 中的属性action中删除notnull="true"就可以了。

You can keep your notnull="true" and have cascades work correctly by adding inverse="true" to the foreign key–owning side of the relationship.您可以通过将inverse="true"添加到关系的外键拥有方来保持您的notnull="true"并让级联正常工作。

In your case, this would be on the Action entity:在您的情况下,这将在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. 这是一篇关于 Hibernate 中的inverse如何工作的文章。

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

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