Coldfusion ORM: delete in cascade
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";
}
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.
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.
精彩评论