I'm working on particular application, a spring+hibernate setup. I have one front end in flex, and now I'm trying to add a jstl front end. The lazy initialization exception I get is when I try to call a a method from the jstl model and view controller, which I call from my flex fronend without any trouble.
Here is my web.xml:
<?xml version="1.0" encoding="UTF-8"?>
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<display-name>HTML test project</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>com.bamboo.common.factory.Log4JContextListener</listener-class>
</listener>
<!--<context-param>
<param-name>webAppRootKey</param-name>
<param-value>app.root</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Http Flex Session attribute and binding listener support -->
<listener>
<listener-class>flex.messaging.HttpFlexSession</listener-class>
</listener>
<servlet>
<servlet-name>ApplicationContextFactory</servlet-name>
<servlet-class>com.bamboo.common.factory.ApplicationContextFactory</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>MessageBrokerServlet</servlet-name>
<servlet-class>flex.messaging.MessageBrokerServlet</servlet-class>
<init-param>
<param-name>services.configuration.file</param-name>
<param-value>/WEB-INF/flex/services-config.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MessageBrokerServlet</servlet-name>
<url-pattern>/messagebroker/*</url-pattern>
</servlet-mapping>
<!-- JSTL dispatcher-servlet -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
Here I load my application context, which I understand loads the beans from the back end which I will use, set my hibernate configuration and aspect configurations. Afterward I load the flex-messaging servlet, which I understand allows the flex and spring to communicate, and finally the dispatcher-servlet, which will map all the .htm requests to the jstl controllers and views.
Here is my application context:
<?xml version="1.0" encoding="UTF-8"?>
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!-- ========================= GENERAL DEFINITIONS ========================= -->
<!-- ========================= DATA ACCESS OBJECT DEFINITIONS ======================== -->
<bean id="dataSource"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/catWDB</value>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource"><ref local="dataSource"/></property>
<property name="mappingResources">
<list>
<value>/com/bamboo/catW3/catW3.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.connection.pool_size">3</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="transactionManager" lazy-init="true" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>
<bean id="categoryDAOTarget" class="com.bamboo.catW3.DAO.impl.CategoryDAOImpl">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>
<bean id="containerDAOTarget" class="com.bamboo.catW3.DAO.impl.ContainerDAOImpl">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>
<bean id="productDAOTarget" class="com.bamboo.catW3.DAO.impl.ProductDAOImpl">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>
<bean id="productOptionDAOTarget" class="com.bamboo.catW3.DAO.impl.ProductOptionDAOImpl">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>
<bean id="productStatusDAOTarget" class="com.bamboo.catW3.DAO.impl.ProductStatusDAOImpl">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>
<bean id="userDAOTarget" class="com.bamboo.catW3.DAO.impl.UserDAOImpl">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>
<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="velocityProperties">
<value>
resource.loader=class
class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
</value>
</property>
</bean>
<!-- ========================= BUSINESS OBJECT DEFINITIONS ======================== -->
<bean id="catalogFacadeTarget" class="com.bamboo.catW3.business.impl.CatalogFacadeImpl">
<property name="categoryDAO"><ref local="categoryDAOTarget"/></property>
<property name="containerDAO"><ref local="containerDAOTarget"/></property>
<property name="productDAO"><ref local="productDAOTarget"/></property>
<property name="productOptionDAO"><ref local="productOptionDAOTarget"/></property>
<property name="productStatusDAO"><ref local="productStatusDAOTarget"/></property>
<property name="userDAO"><ref local="userDAOTarget"/></property>
</bean>
<!-- ================================ VIEW RESOLVER ===================================== -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<!-- ========================= ASPECT CONFIGURATION ======================== -->
<bean id="catalogFacade" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref local="transactionManager"/>
</property>
<property name="target">
<ref local="catalogFacadeTarget"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="get*">PROPAGATION_SUPPORTS,readOnly</prop>
<prop key="contains*">PROPAGATION_SUPPORTS,readOnly</prop>
<prop key="login*">PROPAGATION_SUPPORTS,readOnly</prop>
</props>
</property>
</bean>
So what I'm trying to do, and assumed I was doing up until now, is share the catalogFacadeTarget bean with Flex, and with the controllers from the jstl setup. So I can call the same methods I call from flex, now from the controllers with the model and view methods. I thought that this way I would not encounter any problems. Here is my dispatcher-servlet:
<?xml version="1.0" encoding="UTF-8"?>
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- URL mapping -->
<bean id="urlMap"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<props>
<prop key="/welcome.htm">welcome</prop>
<prop key="/welcome">welcome</prop>
<prop key="/catalog.htm">catalog</prop>
<prop key="/catalog">catalog</prop>
</props>
</property>
</bean>
<!-- Controllers -->
<bean name="welcome"
class="com.bamboo.catW3.business.impl.Welcome">
<property name="successView">
<value>welcome</value>
</property>
<property name="catalogFacadeImpl" ref="catalogFacadeTarget"/>
</bean>
<bean name="catalog"
class="com.bamboo.catW3.business.impl.Catalog">
<property name="successView">
<value>catalog</value>
</property>
<property name="catalogFacadeImpl" ref="catalogFacadeTarget"/>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
Now this was working fine, even though my intellij ide highlights catalogFacadeTarget reference and says that I can't resolve the bean, which would be great: if anyone knows how to make him understand, or maybe make me understand:
<property name="catalogFacadeImpl" ref="*catalogFacadeTarget*"/>
This was working fine for simple objects that do not have complicate relationships, for example I have a table USER that does not relate to anyone, and I can do this from the model and view method on the jstl controller:
List users = catalogFacadeImpl.getUserList();
No problem, I get the list of users and print it. However if I try to make this on a more complicated object, that has relationships with itself and stuff, like with the Object CATEGORY:
List categories = catalogFacadeImpl.getCategoryList();
I get the exceptcion:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.bamboo.catW3.domain.Category.categories, no session or session was closed
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:488)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:431)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
This would usually be a problem with the hbm and the way relationships are configured, however, this can't be the case, because from flex I call this very same method and have no trouble. So what I guess is that I'm not sharing the bean as I though i was. Not that would not give me this error either.
I'm really confused now, any suggestion on where to look would be deeply appreciated. Thank you very much, tried to post the hbm just in case it helps somehow, but was to long for this post.