Hibernate version:3.3
Code between sessionFactory.openSession() and session.close():
Spring 2.0.7 HibernateTemplate and HibernateTransactionManager
Name and version of the database you are using:
H2 1.0.59
Hi all,
First of all: apologies if this belongs on the Spring forum; hopefully it will be useful for someone with similar problems. I have this case:
I start a transaction with Spring HibernateTransactionManager, then do the following inside the transaction (part1):
Code:
1. hibernateTemplate.save(object)
2. object.setValue(newValue)
3. hibernateTemplate.execute [...] { return [Query where object.value == newValue]}
Part 1 works just fine: just before the query is executed, the session is flushed with the changes (value = newValue). The query returns the same object that was saved in (1) - the object id is the same, and the value is updated to newValue. If I end the transaction at this point and inspect the database, value is newValue in the database.
Part2 (the same transaction):
Code:
4. object.setValue(anotherValue)
5. hibernateTemplate.execute [...] { return [Query where object.value == anotherValue]}
Here's the strange thing: Just before the sql for the query in (5) executes .... there's no flush of the session with (value == anotherValue). However, the query executes and returns the same object that was saved in (1), and the object has value==anotherValue. When the transaction commits and I check the database, value==newValue.
Obviously, I'd expect the session to be flushed just before I ran the second query as well.
I tried different things without success:
1) Make the transaction propagation nested
2) Manually flush the transaction after the value was set with a new value. Did this with hibernateTemplate.flush(). Saw from the sql log that nothing was flushed.
3) Debugged the transaction to check the hibernate Sessions identity at all points it was used. The id was the same during the whole transaction.
4) Split the operation in two transaction - one for part 1 and one for part 2. That worked as expected (but is however not possible for me to do as this is a unit test which I need to roll back).
Any help appreciated! If you need mappings or code examples, I'll post it (left out for readability).