简体   繁体   English

Hibernate 删除级联

[英]Hibernate Delete Cascade

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".我已经用“all-delete-orphan”的级联属性映射了关系。

In my DB the relation is mapped with a project_id (FK) field on the questions table.在我的数据库中,关系映射到问题表上的 project_id (FK) 字段。 this field cannot be null since I don't want a Question without a Project.这个字段不能是 null 因为我不想要没有项目的问题。

When I do session.delete(project) it throws an exception saying that project_id cant be null , but if I remove the not-null constraint to that field, the deletion works nice.当我执行session.delete(project)时,它会抛出一个异常,说project_id不能是null ,但是如果我删除该字段的not-null约束,则删除效果很好。

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;不会从数据库中删除 c; it will only remove the link to p (and cause a NOT NULL constraint violation, in this case).它只会删除到 p 的链接(并在这种情况下导致 NOT NULL 约束违规)。 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.因此,如果我们从集合中删除一个 Child,我们确实希望它被删除。 For this, we must use cascade="all-delete-orphan".为此,我们必须使用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.关于反向的东西,我相信这只会决定 sql 的生成方式,有关更多信息,请参阅此文档

One thing to note is, have you got需要注意的一件事是,你有没有

not-null="true"

on the many-to-one relationship in your hibernate config?休眠配置中的多对一关系?

One strategy is to mark the foreign-key in the database with on-delete-cascade, so as soon as NHibernate tells the database to delete a project, the database itself will cascade the deletes.一种策略是用 on-delete-cascade 标记数据库中的外键,这样只要 NHibernate 告诉数据库删除一个项目,数据库本身就会级联删除。 Then you have to tell NHibernate that the database itself does a cascade delete.然后你必须告诉 NHibernate 数据库本身进行级联删除。

If anyone have situation like this then make sure you actually have set proper CascadeType (one of: ALL, REMOVE, DELETE).如果有人遇到这种情况,请确保您确实设置了正确的 CascadeType(其中之一:ALL、REMOVE、DELETE)。 It needs to be in the entity that you try to delete:它需要在您尝试删除的实体中:

public class Project {

    @Id
    private long id;

    @OneToMany(mappedBy = "project", cascade = {CascadeType.REMOVE})
    public List<Question> questions;
}

Deleting should work whether there is NOT NULL constraint on foreign key or not just with:无论外键是否存在 NULL 约束,删除都应该有效:

session.delete(project);

The delete is occurring on the Project first and cascading to the Question, but the Project delete includes a nulling of the project_id in the Questions (for referential integrity. You're not getting an exception on the deletion of the Question object, but because the cascade is trying to null the FK in the Question(s).删除首先发生在项目上并级联到问题,但项目删除包括问题中 project_id 的清零(为了参照完整性。您不会在删除问题对象时获得异常,但因为级联试图将问题中的 FK 归零。

Looking at " Java Persistence with Hibernate ", I think that what you really want a cascade type of delete or remove, not delete-orphans.看看“ Java Persistence with Hibernate ”,我认为你真正想要的是级联类型的删除或删除,而不是删除孤儿。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM