Coldfusion ORM:级联删除

发布于 2024-11-10 15:38:52 字数 1594 浏览 2 评论 0原文

我不是冷融合方面的专家,我打电话给你帮助是因为我在拉扯我的头发!

我例外地删除了具有两个一对多关系的实体“操作”:“文本”和“奖励”。

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

但是,当操作同时具有文本和奖励时,我收到此错误:

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

为什么 Hibernate 在删除操作之前不删除奖励?就像对文本所做的那样?

感谢

操作实体:

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

}

I'm not an expert in coldfusion orm and I call your help because I'm pulling my hair!

I'm excepting to delete an entity 'Action' that has 2 relationship one-to-many, 'Texts' and 'Bonus'.

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

错爱 2024-11-17 15:38:52

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

所以..从 Bonus.cfc 的属性 action 中删除 notnull="true" ,一切就都准备好了。

Somehow Hiberate would first set the entity to Null (become orphan), then delete the orphans.

So.. remove notnull="true" from property action in Bonus.cfc and you're all set.

少女的英雄梦 2024-11-17 15:38:52

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

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

You can keep your notnull="true" and have cascades work correctly by adding inverse="true" to the foreign key–owning side of the relationship.

In your case, this would be on the 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"
             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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文