-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: load() before delete() changes behavior
PostPosted: Wed Jan 05, 2011 4:27 pm 
Regular
Regular

Joined: Thu Oct 19, 2006 12:07 pm
Posts: 75
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>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.