Using hibernate 3.6.0.Final I noticed that load()ing an object and delete()ing it behaves differently than just doing a delete().
The Session.delete() API doc states:
Remove a persistent instance from the datastore. The argument may be an instance associated with the receiving Session or a transient instance with an identifier associated with existing persistent state. This operation cascades to associated instances if the association is mapped with cascade="delete".
It does not suggest that the operation is vastly different in one case compared other.
When doing just session.delete(new Parent(del_id)), this happens:
- update CHILDREN set parent_id=null where parent_id=del_id
- delete from PARENTS where id=del_id
When doing session.delete(session.load(Parent.class, del_id)) this happens:
- select parent0_.id as id0_0_, (and other properties...) from PARENTS parent0_ where parent0_.id=del_id
- select child0_.parent_id , id, name, parent_id, step_parent_id from CHILDREN child0_ where child0_.parent_id=del_id
- select stepchild0_.stepparent_id as my4_0_1_, ... from CHILDREN stepchild0_ where stepchild0_.stepparent_id=del_id
- update CHILDREN set parent_id=null where parent_id=del_id
- delete from CHILDREN where id=1 // from the first SELECT
- delete from CHILDREN where id=2 // from the first SELECT
- delete from PARENTS where id=del_id
( I shortened here the column list in SELECT and replaced ? with del_id)
So it is quite a difference. Is this a bug/omission? A case of bad documentation?
I missed something?
I have two one-to-many relations from Parent to Child, one is inverse="true" (the stepchildren).
Here are the mappings:
Code:
<class name="Parent" table="PARENTS">
<id name="id" column="id">
<generator class="sequence" />
</id>
<property name="name" />
<set name="Children" cascade="delete">
<key column="parent_id" />
<one-to-many class="Child" />
</set>
<set name="stepChildren" inverse="true" cascade="delete">
<key column="step_parent_id" />
<one-to-many class="Child" />
</set>
</class>
<class name="Child">
<id name="id" column="id">
<generator class="sequence" />
</id>
<property name="name" />
<many-to-one name="parent" column="parent_id" update="false"
insert="false" />
<many-to-one name="stepParent" column="step_parent_id" />
</class>