Spring 3.2 + Hibernate 4 OpenSessionInViewFilter

前端 未结 2 1286
夕颜
夕颜 2021-02-03 13:57

I\'m a Spring newbie trying my first app. My hibernate gets closed before the view is rendered and having problems with lazy loaded properties (expected behavior). I\'ve added

相关标签:
2条回答
  • 2021-02-03 14:40

    To solve the problem in my case it was just missing this line

    in the application-context file.

    The @Transactional annotation over a method was not taken into account.

    Hope it will help someone

    0 讨论(0)
  • 2021-02-03 14:48

    My controllers where initialized twice, the same config was used by the root ApplicationContext and by the FrameworkServlet. I've had two contexts initialized. I've created a config for the root context named springapp.xml and moved all my middle tier configuration there and left my web tier configuration in springapp-servlet.xml

    My web.xml now looks like this and everything works fine:

          <context-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>
                    /WEB-INF/springapp.xml
            </param-value>
            </context-param>
    
         <listener>
                <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
            </listener>
    
    <filter>
          <filter-name>hibernateFilter</filter-name>
          <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
          <init-param>
             <param-name>sessionFactoryBeanName</param-name>
             <param-value>sessionFactory</param-value>         
          </init-param>      
       </filter>
    
       <filter-mapping>
         <filter-name>hibernateFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
       </filter-mapping> 
    
        <servlet>
            <servlet-name>springapp</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                    <param-name>contextConfigLocation</param-name>
                    <param-value>/WEB-INF/springapp-servlet.xml</param-value>
            </init-param>
    
            <load-on-startup>1</load-on-startup>
          </servlet>
    
    
    
          <servlet-mapping>
            <servlet-name>springapp</servlet-name>
            <url-pattern>/</url-pattern>
          </servlet-mapping>
    
    0 讨论(0)
提交回复
热议问题