Spring Data JPA的懒加载问题

版权声明:[blog:xuchuruo.cn] 本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a243293719/article/details/78288537

有些数据需要使用到,所以要解决懒加载.就用了最方便的方法,配置过滤器OpenSessionInViewFilter

<!--OpenSessionInViewFilter-->
    <filter>
        <filter-name>openSessionInViewFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>openSessionInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

结果报了以下错误:

HTTP Status 500 - No bean named 'sessionFactory' is defined


type Exception report

message No bean named 'sessionFactory' is defined

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

exception

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'sessionFactory' is defined
	org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:687)
	org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1168)
	org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:281)
	org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
	org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:962)
	org.springframework.orm.hibernate5.support.OpenSessionInViewFilter.lookupSessionFactory(OpenSessionInViewFilter.java:190)
	org.springframework.orm.hibernate5.support.OpenSessionInViewFilter.lookupSessionFactory(OpenSessionInViewFilter.java:175)
	org.springframework.orm.hibernate5.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:126)
	org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.65 logs.


Apache Tomcat/7.0.65

然而控制台没有报错,说缺少sessionFactory,翻了下数据库配置文件,发现根本没有使用到sessionFactory

因为使用的是Spring Date JPA , 没有使用Hibernate

所以如果使用JPA ,应该配置下面的过滤器就没问题了

    <!--Spring Date JPA解决懒加载使用OpenEntityManagerInViewFilter-->
    <filter>
        <filter-name>OpenEntityManagerInView</filter-name>
        <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>OpenEntityManagerInView</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


展开阅读全文

没有更多推荐了,返回首页