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.  [ 2 posts ] 
Author Message
 Post subject: list.clear() with cascade=all
PostPosted: Thu Jan 12, 2006 7:59 am 
Newbie

Joined: Thu Jan 12, 2006 7:41 am
Posts: 10
Location: Lyon - France
When I call clear() method on a mapped collection (many-to-one unidirectional association) with cascade=all, children are not remove from the collection in the database. When I set cascade=all-delete-orphan, children are removed from the collection, and also deleted in the database, fine. However, I just want to delete the association, not the children item (that's why I try to use cascade=all). Any idea to that problem ?

Java code
Code:
         
session = getSession();
tx = session.beginTransaction();
Un un = (Un)session.get(Un.class, new Long(i));// => two elements in the collection
un.getDeux().clear();// => no updates !
tx.commit();


Mapping

Code:
   <class name="Un" table="UN">
      <id name="id" column="ID">
         <generator class="increment"/>
      </id>
      <property name="code" column="CODE"/>
      <list name="deux" cascade="all" inverse="false">
         <key column="UN_ID"/>
         <list-index column="POSITION"/>
         <one-to-many class="Deux"/>
      </list>
   </class>
   <class name="Deux" table="DEUX">
      <id name="id" column="ID">
         <generator class="increment"/>
      </id>
      <property name="code" column="CODE"/>
   </class>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 12:04 pm 
Newbie

Joined: Thu Jan 12, 2006 7:41 am
Posts: 10
Location: Lyon - France
OK, I found the following workaround : 1. make the association bidirectionnal 2. set parent to null on the removed children

New Java code
Code:
session = getSession();
tx = session.beginTransaction();
Un un = (Un)session.get(Un.class, new Long(i));// => two elements in the collection
for (Deux d : un.getDeux()) {
  d.setUn(null);// => update child link
}
un.getDeux().clear();// => no updates

tx.commit();


New Mapping
Code:
<class name="Un" table="UN">
      <id name="id" column="ID">
         <generator class="increment"/>
      </id>
      <property name="code" column="CODE"/>
      <list name="deux" cascade="all" inverse="false">
         <key column="UN_ID"/>
         <list-index column="POSITION"/>
         <one-to-many class="Deux"/>
      </list>
   </class>
   <class name="Deux" table="DEUX">
      <id name="id" column="ID">
         <generator class="increment"/>
      </id>
      <property name="code" column="CODE"/>
      <many-to-one name="un" class="Un" column="UN_ID" cascade="save-update" insert="false" update="false" not-null="false" />
   </class>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.