Hibernate is not deleting orphans when I set the collection to null, although orphans are deleted when the collection is cleared. I have the following associations.
D - entity, contains a single embedded E
E - embedded object, contains one to many relationship with F (cascade type all,DELETE_ORPHAN)
F - entity, contains a collection of Strings
Note that because E is embedded in D, in the database E has no identity outside of D. Thus I will refer to D/E as a unit. Also notice that F contains a collection of Strings. Due to limitations in hibernate, this means that F must be an entity, not an embeddable value type. The cascade types of the collection of Fs in E include all and DELETE_ORPHAN.
If I want to remove the collection of Fs from D/E, I could explicitly clear all the Fs out of the collection, like this.
D d = //get a reference to a D
E e = d.getE();
Set<F> fs = e.getFs();
fs.clear();
//... update d in the session
This correctly removes all the database rows for Fs pertaining to D/E from the F table and the pertinent rows from the D/E to F join table, as I would expect. However, say I wanted to set the collection of Fs to null, like this.
D d = //get a reference to a D
E e = d.getE();
e.setFs(null);
//... update d in the session
Although this deletes the relationship between D/E and F in the join table, it does NOT remove the database rows for the Fs pertaining to D/E from the F table. Those database rows are now orphaned.
Is there a hibernate setting that allows one to set the collection of Fs to null and have hibernate realize that all Fs in that original collection are now orphaned?