2012-03-07 69 views
1

我想使用OpenEntityManageInViewFilter,但我一直有LazyInitializationException。人們在EntityManager初始化兩次時遇到了類似的問題 - 但這似乎不是我的情況。在我可以看到的例外日誌中,Filter正確地啓動。LazyInitializationException即使與OpenEntityManagerInViewFilter

的web.xml:

<web-app version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation=" 
     http://java.sun.com/xml/ns/j2ee 
     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 

    <servlet> 
     <servlet-name>web-application</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>web-application</servlet-name> 
     <url-pattern>/*</url-pattern> 
    </servlet-mapping> 

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>WEB-INF/web-application-servlet.xml</param-value> 
    </context-param> 

    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <filter> 
     <filter-name>openEntityManageInViewFilter</filter-name> 
     <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class> 
    </filter> 

    <filter-mapping> 
     <filter-name>openEntityManageInViewFilter</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 

</web-app> 

Web的應用程序servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-3.1.xsd 
      http://www.springframework.org/schema/tx 
      http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"> 

    <import resource="classpath:applicationContext.xml"/> 
    <context:property-placeholder location="classpath:application.properties"/> 

    <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> 
     <property name="viewNames" value="*" /> 
     <property name="cache" value="false"/> 
     <property name="prefix" value=""/> 
     <property name="suffix" value=".ftl"/> 
     <property name="exposeSpringMacroHelpers" value="false"/> 
    </bean> 

    <bean id="handlerAdapter" autowire="byType" class="org.maite.controller.router.HandlerAdapter" /> 
    <bean id="handlerMapping" autowire="byType" class="org.maite.controller.router.HandlerMapping" /> 
</beans> 

的applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-3.1.xsd 
      http://www.springframework.org/schema/tx 
      http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"> 


    <tx:annotation-driven /> 
    <context:annotation-config/> 
    <context:spring-configured/> 

    <bean id="entityManagerFactory" 
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
     <property name="persistenceUnitName" value="persistenceUnit"/> 
    </bean> 

    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> 
</beans> 

和異常(你可以看到OpenEntityManagerInView需要部分堆棧跟蹤):

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: org.goout.model.Event.schedule, no session or session was closed 
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894) 
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 
    org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:147) 
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) 
root cause 

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: org.goout.model.Event.schedule, no session or session was closed 
    org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:393) 
    org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:385) 
    org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:378) 
    org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:112) 
    org.hibernate.collection.internal.PersistentSet.toArray(PersistentSet.java:188) 
    java.util.ArrayList.<init>(ArrayList.java:151) 
    freemarker.template.SimpleSequence.<init>(SimpleSequence.java:162) 
    freemarker.template.DefaultObjectWrapper.wrap(DefaultObjectWrapper.java:117) 
    freemarker.template.WrappingTemplateModel.wrap(WrappingTemplateModel.java:134) 
    freemarker.template.SimpleHash.get(SimpleHash.java:224) 
    freemarker.core.Dot._getAsTemplateModel(Dot.java:76) 
    freemarker.core.Expression.getAsTemplateModel(Expression.java:89) 
    freemarker.core.IteratorBlock.accept(IteratorBlock.java:94) 
    freemarker.core.Environment.visit(Environment.java:221) 
    freemarker.core.MixedContent.accept(MixedContent.java:92) 
    freemarker.core.Environment.visit(Environment.java:221) 
    freemarker.core.Environment.visit(Environment.java:310) 
    freemarker.core.BlockAssignment.accept(BlockAssignment.java:83) 
    freemarker.core.Environment.visit(Environment.java:221) 
    freemarker.core.MixedContent.accept(MixedContent.java:92) 
    freemarker.core.Environment.visit(Environment.java:221) 
    freemarker.core.Environment.process(Environment.java:199) 
    freemarker.template.Template.process(Template.java:237) 
    org.springframework.web.servlet.view.freemarker.FreeMarkerView.processTemplate(FreeMarkerView.java:366) 
    org.springframework.web.servlet.view.freemarker.FreeMarkerView.doRender(FreeMarkerView.java:283) 
    org.springframework.web.servlet.view.freemarker.FreeMarkerView.renderMergedTemplateModel(FreeMarkerView.java:233) 
    org.springframework.web.servlet.view.AbstractTemplateView.renderMergedOutputModel(AbstractTemplateView.java:167) 
    org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262) 
    org.maite.DispatcherServlet.doService(DispatcherServlet.java:38) 
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882) 
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 
    org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:147) 
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) 

回答

3

你必須在this question描述的,這意味着你有2種的EntityManager豆同樣的問題 - 這是造成混亂。

基本上你需要刪除

<import resource="classpath:applicationContext.xml"/> 

web-application-context.ml,分區您的豆,使得網絡層和應用豆在不同的上下文文件,並確保您的組件掃描每個拿起正確的豆子。這可能需要將類移動到不同的包中,如果沒有關於項目結構的更多信息,很難提供建議。

最後引用來自web.xml的兩個上下文

+0

謝謝,它幫助! – 2012-03-15 16:36:38

+0

很高興能有所幫助。 – sw1nn 2012-03-15 17:38:07

2

您能否提供您的DAO類,它無法初始化另一個DAO類的集合?

嘗試將這些參數放入過濾器中的web.xml中。

<init-param> 
     <param-name>entityManagerFactoryBeanName</param-name> 
     <param-value>entityManagerFactory</param-value> 
</init-param> 
相關問題