Reputation: 38420
Let's say I have two entities, a Post
and a Comment
(in ColdFusion):
component persistent="true" table="post"
{
property name="Id" fieldtype="id";
property name="Comments" fieldtype="one-to-many" cfc="Comment" fkcolumn="post_id" cascade="all";
}
component persistent="true" table="comment"
{
property name="Id" fieldtype="id";
property name="Post" fieldtype="many-to-one" cfc="Post" column="post_id";
}
Post
has a collection of Comments
. Now I'd like to delete a Post
, and have the Comments
automatically deleted as well. I've tried the straightforward method:
var post = EntityLoadByPK("Post", 13);
EntityDelete(post);
But I'm getting a Hibernate error that says that post_id
cannot be set to null. What am I doing wrong, and how can I fix this issue?
Upvotes: 4
Views: 388
Reputation: 190
You need to adjust your mappings. Try making the Post property of comment not null and marking the Comments property of post as inverse.
component persistent="true" table="post"
{
property name="Id" fieldtype="id";
property name="Comments" fieldtype="one-to-many" cfc="Comment" fkcolumn="post_id" cascade="all" inverse="true";
}
component persistent="true" table="comment"
{
property name="Id" fieldtype="id";
property name="Post" fieldtype="many-to-one" cfc="Post" column="post_id" notnull="true";
}
Upvotes: 2
Reputation: 32915
You'll have to make post_id in Comment table nullable in your DB. That's how hibernate does cascade delete. It'll set all Comments with post_id = 13 as null, then delete all comments where post_id IS NULL
Upvotes: 1