O.k, I found the problem!
The thing is that I'm using Spring (sorry, forgot to mention) and didn't realise that the convenience methods such as getHibernateTemplate().get() are only for single step actions. By modifying an object after performing such an operation, you are already outside the session!
I did suspect something like this, but then tried to use an explicit call to getSession() to retrieve a session and then performed the operations within the context of that session. Trouble is, I didn't realise that you need to set up an Interceptor or use a call-back for this to work in Spring.
The thing that made all of this confusing was that the symptoms were the same in both cases: I could load data but not update it. The reasons, however, were quite different. Still, this experience made me understand how Hibernate works with Spring, especially in terms of thread-bound Sessions which are not obvious.
This stuff isn't clear from the "simple" example applications which seem to limit themselves to single step operations and hence make extensive use of getHibernateTemplate().find getHibernateTemplate().save, etc. I'm not sure how realistic this is, and might result in bad programming practices where developers are unnecessarily introducing long transactions.
Dominic.
|