How to delete a collection in Hibernate intermediate?

Suppose 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 series of comments. Now I want to delete the post and delete the comments automatically. I tried the straightforward method:

var post = EntityLoadByPK("Post", 13);< br />EntityDelete(post);

But I received a Hibernate error stating that post_id cannot be set to null. What am I doing wrong, and how can I solve this problem?

You need to adjust the mapping. Try to make the Post attribute of the comment not null, and set the Comment of the post The property is marked 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";
}< br />
component persistent="true" table="comment"
{
property name="Id" fieldtype="id";
property name="Post" fieldtype ="many-to-one" cfc="Post" column="post_id" notnull="true";
}

Suppose 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="man y-to-one" cfc="Post" column="post_id";
}

Post has a series of comments. Now I want to delete the post and automatically delete the comments. I tried straightforward Method:

var post = EntityLoadByPK("Post", 13);
EntityDelete(post);

But I received a Hibernate Error, stating that post_id cannot be set to null. What am I doing wrong, and how can I solve this problem?

You need to adjust the mapping. Try to make the Post attribute of the comment not null, and mark the Comment attribute of the 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";
}

Leave a Comment

Your email address will not be published.