I am working on a web application. We use Spring for transaction management, have an OpenSessionInView filter and use optimistic locking with Hibernate versioning.

When a user edits a record we put the relevant object into the HTTP session and reattach it when they submit / save their changes. At this point we need access to some of the proxied objects and lazy loaded collections that were not initialised when the object was loaded, and also to do a version check.

Testing has shown that we can access proxied objects and do the version check by reattaching the detached object with lock(object, LockMode.READ) and specifying cascade="lock" on those relationships we want to access.

But this does not work for lazy loaded collections. To access them we also need to call update(object) and specify cascade="save-update" at all points along to path to the collection.

So for example I load a Timesheet and detach it. I reattach it to a new session, and want to access a collection on the Timesheet's owner (a Resource), to which the Timesheet is mapped with a many-to-one. To do this I need to specify cascade="update" on the many-to-one between Timesheet and Resource and on the Resource's collection.

This has the side-effect though that the version number of the Resource is incremented. As a Resource is used extensively throughout the system this scenario will repeat itself to the extent that many unrelated actions will try and increment it's version number, causing unnecessary lock failures. This would be inappropriate because it is not changing in these cases.

So firstly, is this expected behaviour of lock() and update()? Should I be able to access lazy loaded collections using just lock()?