左右java的“No bean named 'sessionFactory' is defined ”  现在经常出去SHH或在其框架内Sping+JPA使用底部HIbernate annotation 的实现;出现异常的原因是找不到sessionFactory bean.

原因有例如以下:

1、在Many-to-one  或是many-to-many 时候 我们设置了fetchType=“lazy” 延时载入。

解决这个问题的方法在web.xml中,增加  openSessionInView过滤器 ,OpenSessionInViewFilter的主要功能是用来把一个Hibernate Session和一次完整的请求过程相应的线程相绑定。目的是为了实现"Open Session in View"的模式。

比如:它同意



在事务提交之后延迟载入显示所须要的对象。

OpenSessionInViewFilter 过滤器将 Hibernate Session 绑定到请求线程中,它将自己主动被 Spring 的事务管理器探測到。所以 OpenSessionInViewFilter 适用于 Service 层使

(当中的filter-class  注意对号入座 我使用的是Hibernate 所以filter-calss :<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>

假设你用的是JPA:<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class> 

<!-- 用于处理延时载入问题  openSessionInView过滤器,该过滤器必须在struts过滤器之前配置-->
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

2、尽管前面已经增加了OpenSessionInViewFilter 。可是也能够是报出sessionFactory not defited;

原因非常easy————名称不匹配,OpenSessionInViewFilter模式使用Bean.xml 里面id="sessionFactory "的Bean 。这时假设你的sessionfactory Bean 不叫做这个名,它自然报出Bean 找不到;

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

版权声明:本文博主原创文章,博客,未经同意不得转载。

java 不寻常的问题 No bean named &#39;sessionFactory&#39; is defined 和 initialize a collection of role的更多相关文章

  1. No bean named &#39;sessionFactory&#39; is defined

    1.错误叙述性说明 严重:Servlet service() for servlet default threw exception . org.springframework.beans.facto ...

  2. 解决BeanNotOfRequiredTypeException: Bean named 'XXX' must be of type XXX, but was actually of type XXX问题

    Java新手,困扰了一下午. 发布时总是报这样一个错误. org.springframework.beans.factory.BeanCreationException: Error creating ...

  3. No bean named 'transactionManager' is defined

    2016-10-20 23:27:17.771 INFO 7096 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped &quo ...

  4. No bean named 'hibernateTemplate' is defined

    1.错误描述 WARN:2015-05-01 15:42:22[localhost-startStop-1] - Exception encountered during context initia ...

  5. org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'transactionManager'

    1.错误描述 五月 01, 2015 2:12:31 下午 org.hibernate.validator.util.Version <clinit> 信息: Hibernate Vali ...

  6. Spring错误之org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'bookService' is expected to be of type 'pw.fengya.tx.BookService' but was actually of type 'com.sun.proxy.$Proxy1

    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cas ...

  7. Bean named '...' is expected to be of type [...] but was actually of type [com.sun.proxy.$Proxy7解决方法

    报错 三月 07, 2017 8:09:52 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepare ...

  8. Springboot2.x 启动报错:Bean named 'xxxService'... but was actually of type 'com.sun.proxy.$Proxy82'

    Springboot 2.0.5 搭建一个新项目启动后报错:Bean named 'xxxService'... but was actually of type 'com.sun.proxy.$Pr ...

  9. Spring Security使用报错 No bean named 'springSecurityFilterChain' is defined

    今天配置spring security时,运行报出No bean named 'springSecurityFilterChain' is defined错误,报错信息如下 严重: Exception ...

随机推荐

  1. JAVA jdbc(数据库连接池)学习笔记(转)

    学习内容: 1.JDBC的含义... JDBC想必学过JAVA的就不会陌生,JDBC到底是什么呢?其实就是由JAVA的一些类和接口构成的API,保存在java.sql和javax.sql..包中的一些 ...

  2. 非常不错 Hadoop 的HDFS (Hadoop集群(第8期)_HDFS初探之旅)

    1.HDFS简介 HDFS(Hadoop Distributed File System)是Hadoop项目的核心子项目,是分布式计算中数据存储管理的基础,是基于流数据模式访问和处理超大文件的需求而开 ...

  3. Microsoft Fakes进行单元测试

    使用Microsoft Fakes进行单元测试(1)   一:什么是单元测试 单元测试是对软件进行准确性验证的步骤.单元测试并不进行整个软件功能的测试,仅仅是对于最小工作单元的测试.一般最小工作单元就 ...

  4. Web Service简单入门示例

    Web Service简单入门示例     我们一般实现Web Service的方法有非常多种.当中我主要使用了CXF Apache插件和Axis 2两种. Web Service是应用服务商为了解决 ...

  5. PHP安装mcrypt.so报错 mcrypt.h not found 的解决的方法

    报错内容:configure: error: mcrypt.h not found. Please reinstall libmcrypt 网上搜索了非常多,包含自带的 yum install lib ...

  6. [渣译文] SignalR 2.0 系列: SignalR 自托管主机

    原文:[渣译文] SignalR 2.0 系列: SignalR 自托管主机 英文渣水平,大伙凑合着看吧…… 这是微软官方SignalR 2.0教程Getting Started with ASP.N ...

  7. Flappy bird源代码(略吊)

    #include<stdio.h> #include<stdlib.h> #include<conio.h> #include<time.h> #inc ...

  8. 8.Eclipse中创建Maven Web项目

     第一步: 创建maven webproject 注意以下一步: 第二步: 继承parent 改动pom.xml文件例如以下 <projectxmlns="http://maven ...

  9. 在Linux下,在网络没有配置好前,怎样查看网卡的MAC地址?

    在Linux下,在网络没有配置好前,怎样查看网卡的MAC地址? 使用 dmesg 与 grep 命令来实际,例如以下: [root@localhost ~]# dmesg | grep eth e10 ...

  10. object-c计划tips-添加到类对象属性

    这个问题从网络包的内容,由于保密问题,我刚才所描述我的业余的想法. 基本的想法: 网络请求,我们应该能够使用基类BaseNetWork, 然后由派生类继承BaseNetWork,并实现一些特殊的方法. ...