Hi all, i need some help figuring out how to use the OpenSessionInViewInterceptor with Hibernate. The problem is that i get org.hibernate.LazyInitializationException when accessing lazily loaded collections in my Controllers. I've been staring at the configuration files for a while and comparing them with various tutorials and examples, and they look OK to me. I'm using Hibernate 3 and Spring 1.2 rc1 running in a web app in Tomcat 5.5.4.
First, applicationContext.xml:The openSessionInViewInterceptor bean is used in my handler mapping in the ${webappname}-servlet.xml file:Code:<!-- Test datasource --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> varous properties ... </bean> <!-- Hibernate session factory, using annotation configuration --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property> <property name="configurationClass"> <value>org.hibernate.cfg.AnnotationConfiguration</value> </property> <property name="dataSource"> <ref bean="dataSource"/> </property> </bean> <bean id="productDao" class="[removed].ProductDaoImpl"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref local="sessionFactory"/> </property> </bean> <!-- Transaction Interceptor set up to do PROPAGATION_REQUIRED on all methods --> <bean id="matchAllWithPropReq" class="org.springframework.transaction.interceptor.MatchAlwaysTransactionAttributeSource"> <property name="transactionAttribute"><value>PROPAGATION_REQUIRED</value></property> </bean> <bean id="matchAllTxInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> <property name="transactionManager"> <ref bean="transactionManager" /> </property> <property name="transactionAttributeSource"> <ref bean="matchAllWithPropReq"/> </property> </bean> <!-- One BeanNameAutoProxyCreator handles beans where we want all methods to use PROPAGATION_REQUIRED --> <bean id="autoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="interceptorNames"> <list> <value>matchAllTxInterceptor</value> </list> </property> <property name="beanNames"> <list> <idref local="shopDao"/> </list> </property> </bean> <bean name="openSessionInViewInterceptor" class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean>My mapped objects are retrieved from DAOs that extend HibernateDaoSupport, e.g.:Code:<bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="interceptors"> <list> <ref bean="openSessionInViewInterceptor"/> </list> </property> <property name="mappings"> <props> ... </props> </property> </bean>
Mapped objects are used in the controllers like this:Code:public class ProductDaoImpl extends HibernateDaoSupport implements ProductDao { public Long create(Product product) { return (Long)getHibernateTemplate().save(product); } ... }Here is the relevant portion of the log for a request that causes a LazyInitializationException:Code:protected ModelAndView onSubmit(...) { ... Product product = (Product)command; // Command object was retrieved from DAO in formBackingObject() product.getTags().clear(); // LazyInitializationException thrown here. ... }Can anyone see what i'm doing wrong? Yes, i know it's FridayCode:DEBUG DispatcherServlet: DispatcherServlet with name ... received request for [/[removed]/productEdit.do] DEBUG DispatcherServlet: Testing handler map [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping@1412c18] in DispatcherServlet with name '[removed]' DEBUG SimpleUrlHandlerMapping: Looking up handler for [/productEdit.do] DEBUG OpenSessionInViewInterceptor: Opening single Hibernate session in OpenSessionInViewInterceptor DEBUG SessionFactoryUtils: Opening Hibernate session DEBUG SessionImpl: opened session DEBUG SessionImpl: setting flush mode to: NEVER DEBUG TransactionSynchronizationManager: Bound value [org.springframework.orm.hibernate3.SessionHolder@1125f92] for key [org.hibernate.impl.SessionFactoryImpl@17fe89] to thread [http-8080-Processor24] DEBUG DispatcherServlet: Testing handler adapter [org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter@1f2fbff] DEBUG ProductEditController: Removing form session attribute [[removed].FORM.command] DEBUG CachedIntrospectionResults: Using cached introspection results for class [[removed].Product] DEBUG BeanWrapperImpl: About to invoke write method [public void [removed].Product.setDescription(java.lang.String)] on object of class [[removed].Product] DEBUG BeanWrapperImpl: Invoked write method [public void [removed].Product.setDescription(java.lang.String)] with value [Purple Cheese Description] DEBUG BeanWrapperImpl: About to invoke write method [public void [removed].Product.setHeadline(java.lang.String)] on object of class [[removed].Product] DEBUG BeanWrapperImpl: Invoked write method [public void [removed].Product.setHeadline(java.lang.String)] with value [Purple Cheese Headline] DEBUG BeanWrapperImpl: About to invoke write method [public void [removed].Product.setName(java.lang.String)] on object of class [[removed].Product] DEBUG BeanWrapperImpl: Invoked write method [public void [removed].Product.setName(java.lang.String)] with value [Purple Cheese Name] DEBUG BeanWrapperImpl: About to invoke write method [public void [removed].Product.setProductId(java.lang.String)] on object of class [[removed].Product] DEBUG BeanWrapperImpl: Invoked write method [public void [removed].Product.setProductId(java.lang.String)] with value [1ABC3] DEBUG ProductEditController: No errors -> processing submit ERROR LazyInitializationException: failed to lazily initialize a collection ([removed].Product.tags) - no session or session was closed org.hibernate.LazyInitializationException: failed to lazily initialize a collection ([removed].Product.tags) - no session or session was closed at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:179) at org.hibernate.collection.AbstractPersistentCollection.write(AbstractPersistentCollection.java:60) at org.hibernate.collection.PersistentSet.clear(PersistentSet.java:215) at [removed].product.ProductEditController.onSubmit(ProductEditController.java:78) at org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission(SimpleFormController.java:248) at org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:243) at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:128) at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:44) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:675) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:623) at org.springframework.web.servlet.FrameworkServlet.serviceWrapper(FrameworkServlet.java:384) at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:353) at javax.servlet.http.HttpServlet.service(HttpServlet.java:709) at javax.servlet.http.HttpServlet.service(HttpServlet.java:802) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:526) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:825) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:731) at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:526) at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80) at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684) at java.lang.Thread.run(Thread.java:595) DEBUG TransactionSynchronizationManager: Removed value [org.springframework.orm.hibernate3.SessionHolder@1125f92] for key [org.hibernate.impl.SessionFactoryImpl@17fe89] from thread [http-8080-Processor24] DEBUG OpenSessionInViewInterceptor: Closing single Hibernate session in OpenSessionInViewInterceptor DEBUG SessionFactoryUtils: Closing Hibernate session DEBUG SessionImpl: closing session ERROR DispatcherServlet: Could not complete request etc.![]()


Reply With Quote