Spring + OpenSessionInView does not work

0

I'm new to Spring and would like to use OpenSessionInView so I do not have to implement my Filter.

web.xml file

<web-app id="WebApp_ID" 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">

    <display-name>Archetype Created Web Application</display-name>

    <!-- openSessionInView SpringMVC -->
    <filter>
        <filter-name>OpenSessionInViewFilter</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>OpenSessionInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- FIM openSessionInView SpringMVC -->

    <context-param>
        <param-name>ApplicationContext</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>

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

    <servlet-mapping>
        <servlet-name>PedidosMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

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

    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>

    <session-config>
        <session-timeout>10</session-timeout>
    </session-config>

    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <!-- <param-value>Production</param-value> -->
        <param-value>Development</param-value>
    </context-param>

</web-app>

File ApplicationContext.xml

<!-- Enable autowire -->
<context:annotation-config />
<context:component-scan base-package="com.pedidos" />

<mvc:annotation-driven />

<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:resources mapping="/webjars/**" location="/webjars/" />
<mvc:resources mapping="/resource/**" location="/resource/theme/" />

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/pedidosmvc" />
    <property name="username" value="root" />
    <property name="password" value="167396052" />
</bean>

<!-- Session Factory Declaration -->
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="com.pedidos.entities" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.enable_lazy_load_no_trans">true</prop>
            <prop key="hibernate.default_schema">pedidosmvc</prop>
            <prop key="format_sql">true</prop>
            <prop key="use_sql_comments">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>

My problem is when using fetch=FetchType.LAZY

It throws the following exception

type Exception report

message org.hibernate.LazyInitializationException: failed to initialize a collection of role: com.pedidos.entities.representante.listUser, no session or session was closed

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.pedidos.entities.representante.listUser, no session or session was closed     org.apache.jasper.servlet.JspServletWrapper.handleJspException (JspServletWrapper.java:556)     org.apache.jasper.servlet.JspServletWrapper.service (JspServletWrapper.java:477)     org.apache.jasper.servlet.JspServlet.serviceJspFile (JspServlet.java:395)     org.apache.jasper.servlet.JspServlet.service (JspServlet.java:339)     javax.servlet.http.HttpServlet.service (HttpServlet.java:731)     org.apache.tomcat.websocket.server.WsFilter.doFilter (WsFilter.java:52)     org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel (InternalResourceView.java:168)     org.springframework.web.servlet.view.AbstractView.render (AbstractView.java:303)     org.springframework.web.servlet.DispatcherServlet.render (DispatcherServlet.java:1246)     org.springframework.web.servlet.DispatcherServlet.processDispatchResult (DispatcherServlet.java:1029)     org.springframework.web.servlet.DispatcherServlet.doDispatch (DispatcherServlet.java:973)     org.springframework.web.servlet.DispatcherServlet.doService (DispatcherServlet.java:895)     org.springframework.web.servlet.FrameworkServlet.processRequest (FrameworkServlet.java:967)     org.springframework.web.servlet.FrameworkServlet.doGet (FrameworkServlet.java:858)     javax.servlet.http.HttpServlet.service (HttpServlet.java:624)     org.springframework.web.servlet.FrameworkServlet.service (FrameworkServlet.java:843)     javax.servlet.http.HttpServlet.service (HttpServlet.java:731)     org.apache.tomcat.websocket.server.WsFilter.doFilter (WsFilter.java:52)     org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.doFilterInternal (OpenSessionInViewFilter.java:151)     org.springframework.web.filter.OncePerRequestFilter.doFilter (OncePerRequestFilter.java:107)

Thanks in advance for the help.

    
asked by anonymous 08.06.2017 / 18:35

0 answers