左右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. js多个物体运动问题2

    问题1 http://www.cnblogs.com/huaci/p/3854216.html 在上一讲问题1,我们可以整理出2点: 1,定时器作为运动物体的属性 2,startMove方法,参数要传 ...

  2. Dom操作高级应用

    table tBodies,tHead,tFoot,rows,cells 一个table有多个tbody oTab.tBodies[0].rows[i].style.background = &quo ...

  3. Cocos2d-x示例:单点触摸事件

    为了让大家掌握Cocos2d-x中的事件机制,以下我们以触摸事件为例.使用事件触发器实现单点触摸事件.该实比如图8-3所看到的,场景中有三个方块精灵,显示顺序如图8-3所看到的,拖拽它们能够移动它们. ...

  4. SQL server 提示“代理XP”被关闭的解决方法

    打开SQL Server Configuration Manager(SQL Server配置管理器),启用SQL Server Agent(SQL Server 代理).

  5. bzoj 3333: 排队计划 解决问题的方法

    [原标题] 3333: 排队计划 Time Limit: 20 Sec  Memory Limit: 128 MB Submit: 161  Solved: 71 [Submit][Status] D ...

  6. 【原创】构建高性能ASP.NET站点之一 剖析页面的处理过程(前端)

    原文:[原创]构建高性能ASP.NET站点之一 剖析页面的处理过程(前端) 构建高性能ASP.NET站点之一 剖析页面的处理过程(前端) 前言:在对ASP.NET网站进行优化的时候,往往不是只是懂得A ...

  7. cocos2dx lua

    对于游戏公司而言.採用游戏脚本lua.python等进行开发也非经常见,可是非常多童鞋对脚本并没有非常熟悉的概念,本篇则向大家简介脚本的用途以及在Cocos2dx基础使用方法: Lua和python这 ...

  8. c#里listview里如何获取点击的是哪一列

    很多时候c#里会用到listview,又会给它添加很多的列,可以设置点击列的标头实现按照这列的内容排序等功能,然而点击不同列排序的方法是不一样的,这时候就需要知道,我到底是点击了哪一列,比如点击名字列 ...

  9. nodejs 递归创建目录

    nodejs没有递归创建目录的方法,以前创建的时候是将目录通过path.sep,然后再一步步判断,这个方法在windows下面遇到盘符的时候,然后蛋疼了.今天又用到了这个功能,突然想到了另外一种方法, ...

  10. T-SQL使用案例——结果数据前面自动补0

    原文:T-SQL使用案例--结果数据前面自动补0 现象: 在开发的过程中,往往需要数字和字符串互转.在转换的过程中,可能需要把1编程00001,这样的格式.实现这种样子是有非常多的方法,本文主要提供一 ...