Coldfusion ORM:级联删除
我不是冷融合方面的专家,我打电话给你帮助是因为我在拉扯我的头发!
我例外地删除了具有两个一对多关系的实体“操作”:“文本”和“奖励”。
当我尝试删除只有文本但没有奖励的操作时,一切都很好。 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不知何故,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 propertyaction
in Bonus.cfc and you're all set.您可以通过将
inverse="true"
添加到关系的外键拥有方来保留notnull="true"
并使级联正常工作。在您的情况下,这将位于
Action
实体上:这是一篇关于
inverse
在 Hibernate 中如何工作的文章。You can keep your
notnull="true"
and have cascades work correctly by addinginverse="true"
to the foreign key–owning side of the relationship.In your case, this would be on the
Action
entity:Here's a write-up on how
inverse
works in Hibernate.