左右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. 算法——字符串匹配Rabin-Karp算法

    前言 Rabin-Karp字符串匹配算法和前面介绍的<朴素字符串匹配算法>类似,也是相应每一个字符进行比較.不同的是Rabin-Karp採用了把字符进行预处理,也就是对每一个字符进行相应进 ...

  2. CF 452A(Eevee-直接试)

    A. Eevee time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  3. Windows下效率必备软件

    AutoHotKey: 神器!神器!神器!当然也得看使用者咯(^__^) 嘻嘻…… Listary: 本地搜索神器,当然还有别的作用,More&More. Launchy : 快速启动安装的应 ...

  4. String.format()【演示具体的例子来说明】

    String.format()[演示样例具体解释] 整理者:Vashon 前言: String.format 作为文本处理工具.为我们提供强大而丰富的字符串格式化功能,为了不止步于简单调用 Strin ...

  5. Mac OS X Yosemite安装Hadoop 2.6记录

    整个安装过程分为四部分: 一.  安装Homebrew 二.  ssh localhost 三. 安装Hadoop已经进行配置文件设置 (伪分布式) 四. 执行栗子 一. 安装Homebrew 採用H ...

  6. 【Andrioid】在Gradle编译时生成一个不同的版本号,动态设置应用程序标题,应用程序图标,更换常数

    写项目的时候常常会遇到下面的情况: 1.须要生成測试版本号和正式版本号的apk 2.測试版本号和正式版本号的URL是不一样的 3.測试版本号和正式版本号的包名须要不一致,这样才干安装到同一部手机上面. ...

  7. Action、Category、Data、Extras知识具体解释

    开头 Intent作为联系各Activity之间的纽带,其作用并不仅仅仅仅限于简单的数据传递.通过其自带的属性,事实上能够方便的完毕非常多较为复杂的操作.比如直接调用拨号功能.直接自己主动调用合适的程 ...

  8. c语言实现hashtable,相似C++的map和iOS的NSDictionary

    跟线性数组和链表不同.HashTable是高速查找的数据结构.本文中的HashTable使用链表处理数组. 该HashTable能够指定table的长度.提供了遍历的方法. 包含table的长度的选择 ...

  9. 排序算法c语言描述---选择排序

    排序算法系列学习,主要描述冒泡排序,选择排序,直接插入排序,希尔排序,堆排序,归并排序,快速排序等排序进行分析. 文章规划: 一.通过自己对排序算法本身的理解,对每个方法写个小测试程序. 具体思路分析 ...

  10. Code Review中的几个提示

    原文:http://coolshell.cn/?p=1302  酷壳 Code Review中的几个提示 陈皓 Code Review应该是软件project最最有价值的一个活动,之前,本站发表过&l ...