6

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.

edbras
  • 4,145
  • 9
  • 41
  • 78

2 Answers2

2

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.

hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
  • I don't understand there is a difference and that Hibernate would be smart enough to determine the difference anyhow even if the link is made null.... – edbras Jan 22 '11 at 23:41
  • @edbas, collection.clear is *very* different that parent.setCollection(null). In one, you tell the collection to do something. In the other, you simply change a reference. – hvgotcodes Jan 23 '11 at 00:34
  • Yes, I understand, but still I would think that hibernate would be smart to deal what that.. My probleem: I have one Categories object that has manyyyy nested Components and a bunch of (value type) collections. When removing a component or a collection I would love that Hibernate would be able to signal that and delete all underlying childs. Note: i use one central table for all these collections as otherwise it perform poor, such that these value type collections are modelled in Hibernate as entities...I am now looking how to "better" model this in Hibernate without creating strange links/bug – edbras Jan 23 '11 at 11:08
  • I added a new question that describes the overall problem: http://stackoverflow.com/questions/4773817/how-to-map-many-value-type-collections-to-one-single-table-in-hibernate – edbras Jan 23 '11 at 12:39
  • does JPA acts the same way ? – Gab Mar 06 '14 at 13:37
0

In the version 5 of Hibernate the problem is very similar but an exception is thrown instead: "A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance". To tackle that I had to switch from null values to empty collections.

The issue in Hibernate is posted here: https://hibernate.atlassian.net/browse/HHH-9940

Vic
  • 1,778
  • 3
  • 19
  • 37