休眠删除级联 [英] Hibernate Delete Cascade

查看:99
本文介绍了休眠删除级联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体[Project]包含其他实体的集合[问题]。

我已将映射关系与级联属性all-delete -orphan。



在我的数据库中,关系映射到问题表上的project_id(FK)字段。这个字段不能为空,因为我不想要一个没有项目的问题。



当我执行session.delete(project)时,它抛出一个异常,说project_id不能null,但是如果我删除了对该字段的非空限制,删除工作就很好。



任何人都知道如何解决这个问题?

解决方案

直接从文档。这就解释了你的问题,我完全相信:

然而,这段代码

  Parent p =(Parent)session.Load(typeof(Parent),pid); 
//从集合中获取一个子元素
IEnumerator childEnumerator = p.Children.GetEnumerator();
childEnumerator.MoveNext();
Child c =(Child)childEnumerator.Current;

p.Children.Remove(c);
c.Parent = null;
session.Flush();

不会从数据库中删除c;它只会删除链接到p(在这种情况下会导致违反NOT NULL约束)。您需要显式删除()Child。

 父p =(父)session.Load(typeof(父),pid ); 
//从集合中获取一个子元素
IEnumerator childEnumerator = p.Children.GetEnumerator();
childEnumerator.MoveNext();
Child c =(Child)childEnumerator.Current;

p.Children.Remove(c);
session.Delete(c);
session.Flush();

现在,在我们的例子中,一个孩子不能没有父母就不存在。所以如果我们从集合中删除一个孩子,我们确实希望它被删除。为此,我们必须使用cascade =all-delete-orphan。

 < set name =Childreninverse = truecascade =all-delete-orphan> 
< key column =parent_id/>
<一对多班=小孩/>
< / set>

编辑:



关于反相的东西,我相信这只会决定如何生成SQL,请参阅此文档更多信息。



有一点需要注意的是,您是否有过

 <$ c $在你的休眠配置中的多对一关系上使用c> not-null =true


I Have one entity [Project] that contains a collection of other entities [Questions].

I have mapped the relation with a cascade attribute of "all-delete-orphan".

In my DB the relation is mapped with a project_id (FK) field on the questions table. this field cannot be null since I don't want a Question without a Project.

When I do session.delete(project) it throws an exception saying that project_id cant be null, but if I remove the not-null constrain to that field, the deletion works nice.

Anyone knows how to solve this?

解决方案

Straight from the documentation. This explains your problem exactly i believe:

However, this code

Parent p = (Parent) session.Load(typeof(Parent), pid);
// Get one child out of the set
IEnumerator childEnumerator = p.Children.GetEnumerator();
childEnumerator.MoveNext();
Child c = (Child) childEnumerator.Current;

p.Children.Remove(c);
c.Parent = null;
session.Flush();

will not remove c from the database; it will only remove the link to p (and cause a NOT NULL constraint violation, in this case). You need to explicitly Delete() the Child.

Parent p = (Parent) session.Load(typeof(Parent), pid);
// Get one child out of the set
IEnumerator childEnumerator = p.Children.GetEnumerator();
childEnumerator.MoveNext();
Child c = (Child) childEnumerator.Current;

p.Children.Remove(c);
session.Delete(c);
session.Flush();

Now, in our case, a Child can't really exist without its parent. So if we remove a Child from the collection, we really do want it to be deleted. For this, we must use cascade="all-delete-orphan".

<set name="Children" inverse="true" cascade="all-delete-orphan">
    <key column="parent_id"/>
    <one-to-many class="Child"/>
</set>

Edit:

With regards to the inverse stuff, i believe this only determines how the sql is generated, see this doc for more info.

One thing to note is, have you got

not-null="true"

on the many-to-one relationship in your hibernate config?

这篇关于休眠删除级联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆