I offer this post more as a how-to than a request for help, but if you know of a better way, do share:
I had a persisted object that I was keeping in the Flow Scope because I needed to manipulate different parts on different requests. The problem I faced is that in a subsequent request when I need to maninpulate the Set my persisted object contained I would encounter an LazyInitializationException (LIE) due to the Session being closed, even though I had been using the OpenSessionInViewFilter (OSIV).
Even though I was using the OSIV and had a Session, that didn't mean that my object would "know" to use that Session to complete its lazy initialization.
So, I had to reattach the object, and this is how I did it: FlowExecutionListener
HTML Code:<bean id="hibernateFlowListener" class="net.cya.spring.webflow.HibernateFlowListener"> <property name="hibernateTemplate"><ref bean="hibernateTemplate"/></property> </bean> <bean name="accountFormController" class="org.springframework.webflow.mvc.FlowController"> <!-- property name="flow"><ref bean="accountFlow"/></property--> <property name="flowExecutionManager"> <bean class="org.springframework.webflow.execution.servlet.ServletFlowExecutionManager"> <property name="flow" ref="accountFlow"/> <property name="listener"><ref bean="hibernateFlowListener"/></property> </bean> </property> </bean>(AccountFormObject is the form backing object I use and it contains an Account, the persisted object with the lazy initialization)Code:public class HibernateFlowListener extends FlowExecutionListenerAdapter { public void loaded(FlowExecutionContext context, Serializable id) { // get our form object AccountFormObject afo = (AccountFormObject)context.getActiveSession().getScope().get("formObject"); // get our current session Session session = hibernateTemplate.getSessionFactory().getCurrentSession(); // reattach our object to session // EDIT: removed the following and replaced with the following line--see kenevel's response below: // session.lock(afo.getAccount(), LockMode.NONE); session.replicate(afo.getAccount(), ReplicationMode.OVERWRITE); } private HibernateTemplate hibernateTemplate; public void setHibernateTemplate(HibernateTemplate hibernateTemplate) { this.hibernateTemplate = hibernateTemplate; } }


Reply With Quote
I myself had a similar issue when my subflows were serialized and then deserialized. Your persistent Set (See hinerate PersistentSet or its superclass), upon serialization, losses the current session, since it is declared as transient. Therefore, there is an explicit need to reattach your object to the current session, especially objects that have collections, like Sets.