您的位置:首页 > 编程语言 > Java开发

java 异常问题 No bean named 'sessionFactory' is defined 和 initialize a collection of role

2014-08-24 11:20 501 查看
关于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;

原因很简单————名称不匹配,OpenSessionInViewFilter模式使用Bean.xml 里面id="sessionFactory "的Bean ,这时如果你的sessionfactory Bean 不叫做这个名,它自然报出Bean 找不到;

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐