Spring + Opensessioninview does not work

Asked

Viewed 38 times

0

I’m a beginner with Spring and would like to use Opensessioninview so I don’t have to implement my Filter.

Filing cabinet web xml.

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

Filing cabinet 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 using fetch=FetchType.LAZY

He presents the following exception

type Exception report

message org.hibernate.Lazyinitializationexception: failed to lazily initialize a Collection of role: com.pedidos.entities.Representante.listUser, no Session or Session was closed

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 for your help.

  • And what’s the problem? There’s some mistake?

  • Good morning Anderson... I added the exception that is returning. Thank you

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.