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

 左右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">


 

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

posted @ 2015-09-17 20:37  phlsheji  阅读(1189)  评论(1编辑  收藏  举报