I have a fairly common environment of spring 1 and hibernate 3 and I am having a problem when attempting the following (psuedo code):
1. start outer transaction
2. x = session.get(Foo.class, id)
3. add an item i to a blank collection on x (the collection is already there, im just adding an item, as opposed to adding a whole new collection)
4. start inner transaction (propagation = REQUIRES_NEW)
5. merge(x); // hibernate writes all properties on x's graph to database
6. commit inner transaction
7. commit outer transaction // hibernate flushes the session associated with this transaction, writing any properties that have changed since (2) to the database.
This results in 2 i's being inserted into that collections backing table, when I only added one. The problem is that hibernate flushes the same change twice. This only happens if x is read in one transaction and then merged in a nested transaction. Note that if the whole collection is replaced, this problem does not occur because each time the collection is flushed hibernate issues a delete statement before the inserts
I am using spring's HibernateTransactionManager and LocalSessionFactoryBean.
Are these kinds of nested transactions just incompatible with the merge operation? Any help would be greatly appreciated!!!
|