I know there are a lot of postings on LazyInitializationException, and I've read many of them, but I haven't been able to fix this problem yet.
I am starting to use Spring and Hibernate 2.1 in a pre-existing web application. I am using the DAO pattern and Karl Baum's Weblog entry "Lazy Initialization and the DAO pattern with Hibernate and Spring". I'm using the Filter method.
I'm under the impression that this filter will open a hibernate session and keep it open for the duration of the request. The first time the objects are accessed, the session is open. But when I try to get the size of a child collection, I get a LazyInitializationException.
I also created a subclass of ContextLoaderServlet. In it I override createContextLoader(), where I return an instance of my own subclass of ContextLoader.
Code:public class MyContextLoader extends ContextLoader { public MyContextLoader() { super(); } private static WebApplicationContext myWebApplicationContext = null; public WebApplicationContext initWebApplicationContext(ServletContext servletContext) throws BeansException { myWebApplicationContext = super.initWebApplicationContext(servletContext); return myWebApplicationContext; } // Allow legacy code the get the instance of the application context so they // can get Spring beans. public static WebApplicationContext getMyWebApplicationContext() { return myWebApplicationContext; } }
I am using the DAO pattern as follows:
DAO Impl:
web.xml:Code:public class MyObjectDaoImpl extends HibernateDaoSupport implements MyObjectDao { /** * Retrieves a List of MyObject objects, using the Spring Framework. * @return List */ public List getObjects() { return getHibernateTemplate().find("from MyObject"); } }
Any help will be greatly appreciated.Code:<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:spring-init-servlet.xml</param-value> </context-param> <filter> <filter-name>hibernateFilter</filter-name> <filter-class>org.springframework.orm.hibernate.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>hibernateFilter</filter-name> <url-pattern>/servlets/com.company.nav.OurCommandDispatcherServlet</url-pattern> </filter-mapping> <servlet> <servlet-name>SpringContextServlet</servlet-name> <servlet-class>com.company.springframework.MyContextLoaderServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet>
Regards,
John[/code]


Reply With Quote