Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove collection with delete-orphan not work with null assignment? :(

I am having problems removing another entity through cascade delete-orphan. It works when I clear the associated set collection, but not when I make the set collection null. Let me explain in detail. The config snippet:

<class name="com.sample.CategoriesDefault" table="cats">
  <id name="id" column="id" type="string" length="40" access="property">
     <generator class="assigned" />
  </id>
  <version name="version" column="objVrs"  unsaved-value="negative"/>

 <set name="bla" lazy="false" cascade="all-delete-orphan" inverse="true">
      <key column="idCats" not-null="true"/>
       <one-to-many class="com.sample.BLA"/>
 </set>

<class name="com.sample.BLA" table="blaTEST">
   <id name="id" column="id" type="string" length="40" access="property">
     <generator class="assigned" />
   </id>
   <version name="version" column="objVrs"  unsaved-value="negative"/>
   <property name="bla" type="string" column="bla"/>
   <many-to-one name="parent" class="com.sample.CategoriesDefault" column="idCats" not-null="true"/>
</class>

My sample code:

Categories cats = new CategoriesDefault();
final Set<BLA> col = new HashSet<BLA>();
col.add(new BLA(cats));
cats.setBla(col);
cats.saveOrupdate(); // will update/insert it in the db.

The following works correctly, that is: all the collection items are all moved from the db.

cats.getBla().clear();
cats.saveOrUpdate();

I think this works as the PersistSet of Hibernate is marked as dirty when calling this method.

The following however doesn't work the same as above like I would want/expect.

cats.setBla(null);
cats.saveOrUpdate();

If I reload the cats item from the db, it still contains the BLA items and no delete statement is generated by Hibernate :(.. Why not??... or is this a bug ? I am using 3.6.0.Final.

like image 486
edbras Avatar asked Jan 22 '11 20:01

edbras


1 Answers

I think its because hibernate uses its own collection implementations (which is why the docs say you MUST declare collections as interfaces, not implementations), and the collection implementations obey the semantics of you transitive persistence settings. So, when you do

cats.getBla().clear()

the getBla() part is the hibernate collection implementation, which knows to remove the children from the session when clear() is called.

When you do

cats.setBla(null);

you haven't removed the collection, you have changed the parent's reference to the collection to null. The collection probably still exists in the session.

like image 171
hvgotcodes Avatar answered Oct 04 '22 22:10

hvgotcodes