Cameron McKenzie wrote:
I think there may be a problem with this approach.
Why the need to refresh these objects. I might suggest you keep your session open a bit longer, and leverage the Open Session in View pattern.
The fact is, you're closing your session too early? Why commit the transaction if the data is still needed? Why close the session if you're not done with it. I think there are some real opportunities for optimization here if you keep your session open.
I am actually using the Open Session in View pattern. But I am using refresh() for a different purpose.
In my scenario there was an optimistic locking error on the previous request which is displayed to the user. On the following page the object graph where the error occurred is loaded again from the database so the user can redo his changes based on the current database version.
To achieve this, the object is refreshed in the request and displayed again on a form.
In some way or other I have to get the current version of the object from the database. The other way would be to load the object completely from the database using Session.get(). But I thought, refresh was built to exactly serve this purpose.
I hope my usage of refresh is clearer now.