Hibernate Delete Cascade

Pablo Fernandez picture Pablo Fernandez · Oct 14, 2008 · Viewed 33.3k times · Source

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 constraint to that field, the deletion works nice.

Anyone knows how to solve this?

Answer

abarax picture abarax · Oct 14, 2008

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?