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 clas开发者_如何学JAVAs="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.
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.
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
精彩评论