English 中文(简体)
如何在Hibernate删除藏书?
原标题:How do I cascade delete a collection in Hibernate?

请允许我说,我有两个实体:PostComment(在冷战时期):

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);

但是,我忘记了一种温和的错误,即:post_id不能被定为无效。 我做了什么错误,我如何解决这个问题?

最佳回答

你们需要调整自己的地图。 要求将评论后的财产不作废,并将职位的评论财产作反常的标记。

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";
}
问题回答

你们不得不在评论表上宣布职位无效。 这样一来,便会删除。 表格列出所有带有员额_id=13号的评论,然后删除员额_id ISNUL的所有评论。





相关问题
Entity Framework with File-Based Database

I am in the process of developing a desktop application that needs a database. The application is currently targeted to SQL Express 2005 and works wonderfully. However, I m not crazy about having ...

Assistance with CakePHP model relationships

How would I represent the following in a CakePHP model? Product ======= product_id .... Cart ==== cart_id .... Carts_Products ============== cart_id product_id quantity

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

How are Models (in MVC) and DAOs supposed to interact?

How are models and DAOs supposed to interact? I m in the process of putting together a simple login module and I m unsure where to put the "business logic." If I put the logic with the data in the ...

Convert date to string upon saving a doctrine record

I m trying to migrate one of my PHP projects to Doctrine. I ve never used it before so there are a few things I don t understand. In my current code, I have a class similar to this: class ...

热门标签