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?
You need to adjust your mappings. Try making the Post property of comment not null and marking the Comments property of post as inverse.