-->
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.  [ 3 posts ] 
Author Message
 Post subject: cannot simple "delete from"
PostPosted: Thu Mar 04, 2004 7:26 am 
Newbie

Joined: Thu Nov 13, 2003 12:03 pm
Posts: 18
Location: France
Following the topic on select and delete :
[url]http://forum.hibernate.org/viewtopic.php?t=926955[/url]

I understand that Hibernate has to do some Lifecycle staff, but then in a one to many relationship if I want to delete all the associated objects without deleting the owner, hibernate has to select all the objects and then delete one by one ! This is not a case of mass process, and I don't want to use JDBC since I would loos the Object/DB mapping of Hibernate ...

Here a sample code :

[code]Session session = sf.openSession();
session.delete("from Kids as detachedObjects where detachedObjects.parent.id = ?", parentid,Hibernate.long);
session.flush();
session.connection().commit();[/code]

Maybe this is wrong way to handle that delete ?
py


Top
 Profile  
 
 Post subject: using all-delete-orphan cascade strategy
PostPosted: Thu Mar 04, 2004 11:26 am 
Newbie

Joined: Thu Nov 13, 2003 12:03 pm
Posts: 18
Location: France
I tried a different strategy :
with a cascade mode sets to "all-delete-orphan"
and the following code

Code:
Session session = sf.openSession();
session.load(parent);
parent.getKids().clear();
session.save(parent);
session.flush();
session.connection().commit();


Mapping:

Code:
<class name="Parent"
        table="PARENT"
        dynamic-update="false"
        dynamic-insert="false"
        discriminator-value="null"    >
       
      <set name="kids"
            lazy="true"
            inverse="true"
            cascade="all-delete-orphan"
            sort="unsorted">

              <key column="PARENT_ID" />

              <one-to-many class="Kid"              />   
        </set>
   </class>
    <class name="Kid"
        table="KID"
        dynamic-update="false"
        dynamic-insert="false" >
        <many-to-one   name="Parent"
            class="Parent"
            cascade="none"
            outer-join="auto"
            update="true"
            insert="true"
            column="PARENT_ID"
            not-null="true" />
    </class>


I get only one select of parent, one select for all kids and one delete for all kids. It is much better, but still Hibernate has to load all the Kids instances in session. I don't think there is any way to avoid that.


Top
 Profile  
 
 Post subject: Re: using all-delete-orphan cascade strategy
PostPosted: Fri Mar 05, 2004 5:15 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
pierre-yves wrote:
but still Hibernate has to load all the Kids instances in session. I don't think there is any way to avoid that.

Correct

_________________
Emmanuel


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

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.