Hi,
Following the one-session-per-request pattern I have a ServletFilter which closes the session after a request. The session factory uses threadlocal.
I have trouble retreiving a persisted object from HTTPSession. The first exception I encoutered was the LazyInitializationException. Okay, storing the object in one session (i.e. first request) and retreiving its collection in another (i.e. second request) doesn't work.
I need to update the object first. And this is the problem. Calling session.saveOrUpdate(instance) as well as session.lock(instance, LockMode.NONE); results in the following error, if I click on the link multiple times (i.e. having more the one thread accessing the object):
Code:
org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions
org.hibernate.collection.AbstractPersistentCollection.setCurrentSession(AbstractPersistentCollection.java:410)
org.hibernate.event.def.OnUpdateVisitor.processCollection(OnUpdateVisitor.java:40)
org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:101)
org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:61)
org.hibernate.event.def.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:55)
org.hibernate.event.def.AbstractVisitor.process(AbstractVisitor.java:123)
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:267)
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:216)
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:502)
org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:494)
org.hibernate.impl.SessionImpl.saveOrUpdate...
Further reading the documentation and understanding that I can't load two intances of the same object in one session, I figured that I need to call merge() instead. However, doing this brings the LazyInitializationException back into place.
What ist the best strategy to store/load persisted objects in HttpSession?