其他分享
首页 > 其他分享> > Spring,Hibernate Lazy Loading,sessionFactory和OpenSessionInViewFilter

Spring,Hibernate Lazy Loading,sessionFactory和OpenSessionInViewFilter

作者:互联网

我正在使用Hibernate的延迟加载,并且即使在我在web.xml中定义了一个过滤器以使用OpenSessionInViewFilter之后,我也得到了sessionFactory缺少的异常

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
    <filter-name>hibernateFilter</filter-name>
    <filter-class>
       org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    </filter-class>
</filter>
<filter-mapping>
    <filter-name>hibernateFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping> 
</web-app>

我的servlet-context.xml具有以下会话和事务管理器定义:

<beans:bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <beans:property name="dataSource" ref="dataSource"/>
    <beans:property name="hibernateProperties">
        <beans:props>
            <beans:prop key="hibernate.dialect">${connection.dialect}</beans:prop>
            <beans:prop key="hibernate.show_sql">true</beans:prop>
       </beans:props>
    </beans:property>
    <beans:property name="annotatedClasses">
        <beans:list>
            <beans:value>example.EntityA</beans:value>
            <beans:value>example.EntityB</beans:value>
        </beans:list>
    </beans:property>
</beans:bean>

<beans:bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <beans:property name="sessionFactory" ref="sessionFactory"/>
</beans:bean>
<tx:annotation-driven transaction-manager="transactionManager" />

我仍然得到以下异常:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'sessionFactory' is defined

我已经尝试定义sessionFactoryBeanName属性但结果不会更改.我究竟做错了什么?

解决方法:

您需要在根Web应用程序上下文中声明sessionFactory(即.WEB-INF/spring/root-context.xml),以使其可用于OpenSessionInViewFilter.

标签:spring,hibernate,lazy-loading
来源: https://codeday.me/bug/20191002/1841861.html