Sping3.1和hibernate4.2集成—— No Session fo

系统 1771 0

在使用spring3和hibernate4.2集成与hibernate3有很多的不同,其中之一就是spring3不在支持HibernateTemplate,而是使用hibernate原生的api,我在集成的时候遇到了如下两个问题。 


问题之一:在使用session.save()方法保存数据时不能成功的保存到数据库  
    这个问题的原因是在获取session时,不能使用openSession()方法,而要使用getCurrentSession()方法 

Java代码   收藏代码
  1.       
  2. @Resource (name= "sf" )  
  3. private  SessionFactory sessionFactory;   
  4. Session session ;  
  5. ...  
  6. session = sessionFactory.getCurrentSession();  



问题之二:使用getCurrentSession时报:hibernate4 org.hibernate.HibernateException: No Session的错误  
    这个问题的原因是session未打开,解决方式: 
    前提记得在service层上面加上@Transaction注释,否则任然会有相同的异常 

方式1-在web.xml中加过滤器 

Java代码   收藏代码
  1. <!-- open session filter -->  
  2. <filter>  
  3.     <filter-name>openSessionInViewFilter</filter-name>  
  4.     <filter- class >org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter- class >  
  5.     <init-param>  
  6.     <param-name>singleSession</param-name>  
  7.     <param-value> true </param-value>  
  8.     </init-param>  
  9. </filter>  


方式2-配置事务的切面 

Java代码   收藏代码
  1. <!-- 用spring管理事务 -->  
  2.    <bean id= "transactionManager"   class = "org.springframework.orm.hibernate4.HibernateTransactionManager" >      
  3.        <property name= "sessionFactory"  ref= "sf" />      
  4.    </bean>    
  5.      
  6.    <!-- 配置使用注解的方式来使用事务 -->   
  7. <tx:annotation-driven transaction-manager= "transactionManager"  />  
  8.   
  9. <!-- 配置那些类的方法进行事务管理,需要aopalliance- 1.0 .jar和aspectjweaver.jar,当前com.neusoft.leehom.service包中的子包,    
  10.                       类中所有方法需要,还需要参考tx:advice的设置 -->    
  11.   
  12.    <!-- 这是事务通知操作,使用的事务管理器引用自 transactionManager -->    
  13.    <tx:advice id= "txAdvice"  transaction-manager= "transactionManager" >    
  14.        <tx:attributes>    
  15.   
  16.            <tx:method name= "insert*"  propagation= "REQUIRED"  />    
  17.            <tx:method name= "update*"  propagation= "REQUIRED"  />    
  18.            <tx:method name= "delete*"  propagation= "REQUIRED"  />    
  19.            <tx:method name= "get*"  propagation= "REQUIRED"  read-only= "true" />    
  20.            <tx:method name= "query*"  propagation= "REQUIRED"  read-only= "true" />    
  21.            <tx:method name= "*"  propagation= "REQUIRED"  />    
  22.        </tx:attributes>    
  23.    </tx:advice>   
  24.      
  25.     <!-- 需要引入aop的命名空间 -->    
  26.    <aop:config>    
  27.        <!-- 切入点指明了在执行Service的所有方法时产生事务拦截操作 -->    
  28.        <aop:pointcut id= "daoMethods"  expression= "execution(* com.tl..serviceimpl.*.*(..))"  />        
  29.        <!-- 定义了将采用何种拦截操作,这里引用到 txAdvice -->    
  30.        <aop:advisor advice-ref= "txAdvice"  pointcut-ref= "daoMethods"  />    
  31.    </aop:config>   
 
 
 
 
 
-------------------分割线------------------------------------
 

在使用Spring mvc + Spring + Hibernate4 配置 的时候,总是出现 No Session found for current thread,仔细检查applicationContext.xml和dispacter-servlet.xml文件,注解、事务 配置 都没有 问题 ,纠结好久。

网上搜了很多方法,都不能 解决

有说加上 < prop   key = "hibernate.current_session_context_class" > thread </ prop >的 配置 有说hibernate4要加上的是 < prop key = "hibernate.current_session_context_class" > org.springframework.orm.hibernate4.SpringSessionContext </ prop >,经过测试,都不能 解决

看了http://blog.csdn.net/qq445422083/article/details/8160387帖子之后,觉得说的有道理,还是事务没有装配。

        试着把 dispacter-servlet.xml中的内容合并到 applicationContext.xml中,采用一个 配置 文件(当然web.xml也要做相应修改),发现并没有报错, 问题解决

        但是自己还是喜欢使用两个 配置 文件,这样结构更清晰。

        分析为什么合并到一起就没 问题 呢,原来 spring的context是父子容器,所以会产生冲突,Controller会首先被扫描装配,而此时的Service还没有进行事务的 配置 ,获得的将是原样的Service(没有经过事务装配,故而没有事务处理能力) ,最后才是applicationContext.xml中的扫描设备进行事务处理。

这样就好办了,让两个 配置 文件各干各的事就可以了。

1 、在Spring主容器中(applicationContext.xml),用 < context:exclude-filter/> 将Controller的注解过滤掉,不扫描装配它。

 

[html]  view plaincopy
 
  1. < context:component-scan   base-package = "com" >   
  2.    < context:exclude-filter   type = "annotation"   expression = "org.springframework.stereotype.Controller"   />   
  3. </ context:component-scan >    

2 而在springMVC 配置 文件中(dispatcher-servlet.xml)将Service和Dao的注解给 过滤 掉 ,只扫描装配 Controller。

 

 

[html]  view plaincopy
 
  1. < context:component-scan   base-package = "com" >   
  2.    < context:include-filter   type = "annotation"   expression = "org.springframework.stereotype.Controller"   />   
  3.    < context:exclude-filter   type = "annotation"   expression = "org.springframework.stereotype.Service"   />  
  4. < context:exclude-filter   type = "annotation"   expression = "org.springframework.stereotype.Repository"   />    
  5.    </ context:component-scan >   

Sping3.1和hibernate4.2集成—— No Session found for current thread


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论