2010-11-14 58 views
1

我正在研究特定的應用程序,一個spring + hibernate安裝程序。我在flex中有一個前端,現在我試圖添加一個jstl前端。我得到的延遲初始化異常是當我嘗試從jstl模型和視圖控制器中調用一個方法時,我從我的flex調用中調用該方法時沒有任何問題。spring + jstl + hibernate懶惰初始化異常,如何在Flex前端找到工作

這裏是我的web.xml:

<?xml version="1.0" encoding="UTF-8"?> 

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd」 版本= 「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> 

這裏我打開我的應用程序上下文,這是我理解的負荷從後端,我會用豆子,把我的Hibernate配置和方面的conf igurations。之後,我加載了彈性消息傳遞servlet,我知道它允許flex和spring進行通信,最後是調度程序servlet,它將把所有的.htm請求映射到jstl控制器和視圖。

這裏是我的應用程序上下文:

<?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> 

所以我想做的事情,並假設我到現在爲止,是共享目錄FacadeTarget bean與Flex,並從jstl設置與控制器。因此,我可以調用我從Flex調用相同的方法,現在從控制使用模型和查看方法。我以爲這樣我就不會遇到任何問題。這裏是我的調度員的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> 

現在這個工作正常,即使我的intellij IDE強調了catalogFacadeTarget引用,並且說我無法解析bean,這會很棒:如果有人知道如何讓他變成並且,也許讓我明白了:

<property name="catalogFacadeImpl" ref="*catalogFacadeTarget*"/> 

這是工作的罰款對於那些沒有複雜的人際關係簡單的物體,比如我有一個不涉及任何表用戶,我可以從這樣做模型和查看方法在jstl控制器上:

List users = catalogFacadeImpl.getUserList(); 

沒問題,我得到用戶列表並打印它。但是,如果我儘量讓這更復雜的物體上,具有與自身的關係的東西,就像對象類別:

List categories = catalogFacadeImpl.getCategoryList(); 

我得到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) 

這通常是hbm和配置關係的問題,然而,這不可能是這樣,因爲從flex我稱這種方法非常相同,並沒有任何問題。所以我猜想我並沒有像我一樣分享這個bean。不,這也不會給我這個錯誤。

我現在非常困惑,任何有關在哪裏尋找的建議都會深表感謝。非常感謝你,試圖張貼HBM,以防萬一它幫助,但是這個帖子很長。

+0

考慮編輯這個問題。這些問題往往不會被閱讀。 – 2010-11-14 22:57:49

+0

未來會記住,得到這個答案,但不是完全閱讀。謝謝 – Ernest 2010-11-15 13:41:12

回答

1

我沒有讀完整個問題,但帶有遠程前端的LazyInitializationException的解決方案是在通過線路發送集合之前初始化集合。因此,要麼:

  • 發送之前定義的所有集合作爲EAGER
  • 呼叫Hibernate.initialize(..)

欲瞭解更多信息,請參閱this answerthis answer。它們是相關的,儘管後者是關於GWT的。

+0

你好Bozho,我得到它的工作感謝您的帖子,所以一百萬感謝您的幫助!如果你沒有通過你提出的兩種解決方案,甚至沒有嘗試過,因爲閱讀第二個答案,它建議嘗試一個名爲OpenSessionInViewFilter的過濾器,它解決了我的問題,甚至認爲我不知道它是如何做的它!以及爲什麼flex前端過去不需要此過濾器。再次遇見你! – Ernest 2010-11-15 13:40:05

0

嘗試將@Transactional添加到您的服務方法中。

相關問題