after some modifications in the code I have found that if I add
@org.hibernate.annotations.Proxy(lazy=false)
to the class, which instance I am trying to make read-only, it works the right way. Looks almost like a bug.
anyway I bumped straight to another issue, as I would like to have in one session one instance with old values (as they are saved in the DB at that tome) and also another read-only copy of that instance, with applied modifications as a sort of preview-new-modifications-and-compare-with-previous-state matter.
so basically I am doing this
Object new = session.load();
session.setReadOnly(new, true);
new = makeTheModifications(new);
Object old = session.load();
but unfortunatelly this is not working, probably due to some cache reasons so even if I have the following code snippet
getSessionFactory().evict(SomeClass.class, itemId);
Session session = SessionFactoryUtils.getSession(getSessionFactory(), false);
session.setCacheMode(CacheMode.IGNORE);
Object rslt = session.load(SomeClass.class, itemId);
getSessionFactory().evict(SomeClass.class, itemId);
rslt = session.load(SomeClass.class, itemId);
the second load() call will not force the item to reload from DB = second SQL statement. Therefore I am not able to get the original state again from DB.
I can not use session.reload() as this will refresh both the copies of the object.
I can not use session.evict() as mentioned earlier.
I can not make directly some sort of copy of the object before applying modifications, because the first load() and the second load() actually happens on completely different places. If you know Spring Web MVC, then first is in formBackingObject method to create the command object (for applying modifications) and the second one is in onSubmit method to create the object with current (old) values from DB. Applying of the modifications to the command object is located (as usually in Spring Web MVC) in the onBind method which is called before onSubmit and after formBackingObject. So if I would be able to make copy in the formBackingObject, I have to solve how to get it into the onSubmit method in some nice and clear way...
|