After a while performing some tests, I concluded that the technique described above is not reliable. Why not? Because of lazy associations.
For instance, I have a Country object that has a collection of State object. If I do:
country.getStates().clear()
on a populated collection, there are changes that must be flushed to the persistent medium. However, the old state (the StoredSnapshot) might be present depending on whether the collection was previously initialized or not. To complicate things even more, the value of such snapshot in that case is not null, but an empty ArrayList.
So, there will be cases in which we'll have the old state in memory and cases that we won't. In the latter that old state should be fetched from the database (that would imply a performance penalty, of course), presumably using another session/transaction.
Now, do you understand why onFlushDirty wasn't working for collections? :P
|