Hello all,
The way Hibernate works generally is that all objects returned by the Hibernate engine are added to the Session cache and when flush is called at the end of the session any dirty objects are automatically persisted.
This is a great feature of Hibernate, but for my application it is not desireable. Basically, I am using my persistent entity objects as user editable "value objects" in a Spring/Tapestry application. Because the the actual persistent objects are out there and more or less exposed to Users, I want to only update objects which I explicity call save or saveOrUpdate on.
So if the session opens and objects A and B are retrieved from the store and both are changed somehow I want to be able to call
Code:
session.save(a);
session.flush();
and know that B wont be persisted.
I know that I can do something like evicting all objects from the session cache whenever they are returned by my DAO methods. So my questions are:
1) Is that the best way to do it?
2) Are there any heinous side effects to calling Session.clear() all the time?
3) Is there a way I can stick this in an interceptor or something so I don't have to call Session.clear() manually everytime I retrieve something from the db? I guess the desired behavior is that Hibernate always gives me back detached objects.
I'm using the session-per-request model.
Thanks to everyone in advance,
Bobby
Hibernate version: 2.x