In my app I start a transaction at servlet request and end the transaction after request processing.
I have several objects which are being saved between the pages and to be able to work with these object I wrote a small method:
Code:
public void ensureAttached() {
if (HibernateSessionFactory.getSession().contains(this))
return;
HibernateSessionFactory.getSession().lock(this, LockMode.NONE);
}
Oddly, this method in several situations throws LazyInitializtionException.
The top of the stack trace looks like this:
Code:
org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:56)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:98)
at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:158)
at my.package.MyPersistentClass$$EnhancerByCGLIB$$92dc9492.ensureAttached(<generated>)
I don't understand why. I mean I know that the owning session was closed, thats exactly why I want to reattach the object! It seems I can't call Session.lock from the object(which belongs to a closed session) itself. why not?