手头有一个项目,使用的是struts2 hibernate3 spring2.5

是之前的老项目了,spring与hibernate的版本都比较低

自己看了最新的spring4与hibernate4,发现hibernateDaoSupport与hibernatetemplate已经不建议使用了。

那么换句话说,事务管理就得自己来整了

直接更新jar包,呵呵,大家试试更新一下自己项目里的老jar包,你就知道有多么蛋疼了。

所以我试着不更新hibernate与spring jar的基础上,使用它们推荐的编码方式。

hibernate的配置

1

使用下面的配置,即不给hibernate.current_session_context_class设值

<prop key="hibernate.hbm2ddl.auto">update</prop>

<prop key="hibernate.show_sql">false</prop>

<prop key="hibernate.format_sql">true</prop>

<!-- 

  <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate3.SpringSessionContext</prop>  

-->

</props>

报下面的错误:

No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

另外如果使用声明式事务管理current_session_context_class就不需要设值了

关于这个current_session_context_class,更详细的资料,大家参见  http://blog.csdn.net/z69183787/article/details/8768421

2

给它赋值,如下:

<prop key="hibernate.current_session_context_class">thread</prop>  

 另外关于事务,我使用的是注解的方法

    <bean id="txManager" 

   class="org.springframework.orm.hibernate3.HibernateTransactionManager">

          <property name="sessionFactory" ref="sessionFactory"/>

    </bean>

    <tx:annotation-driven transaction-manager="txManager"/>

  在action里面,方式头上加上 @Transactional

  (另外,如果方法头没有加 @Transactional 也会报No Hibernate Session bound to thread....的错误)

  代码如下,逻辑写的有点乱,而且也不应该在action里面获得Criteria。

  大家先看技术层面的问题吧

  @SuppressWarnings("unchecked")
@Transactional
public String checkUnCompletedFile(){
      System.out.println("check");
      //获取已经创建但是未完成的冠心病病人档案

     Criteria c=commonService.createCriteria(CdmGXBFile.class);
     c.add(Restrictions.eq("status", "0"));
     c.add(Restrictions.eq("cdmUser", getSessionUser()));
     unCompletedFileList=c.list();

}

commonService最终调用了dao类,dao里面包含sessionFactory,并且通过spring注入

public <T> Criteria createCriteria(Class<T> entityClass) {

Criteria criteria = getSession().createCriteria(entityClass);

return criteria;

}



public Session getSession() {

// 事务必须是开启的(Required),否则获取不到

return sessionFactory.getCurrentSession();

}

运行的时候会报下面的错误:

org.hibernate.HibernateException: createCriteria is not valid without active transaction

at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:338)

at com.sun.proxy.$Proxy44.createCriteria(Unknown Source)

at cdm.core.dao.allbase.GenericBaseCommonDao.createCriteria(GenericBaseCommonDao.java:425)

at cdm.core.service.CommonServiceImpl.createCriteria(CommonServiceImpl.java:271)

at cdm.module.file.gxb.action.BasicInfoAction.checkUnCompletedFile(BasicInfoAction.java:522)

at cdm.module.file.gxb.action.BasicInfoAction$$FastClassByCGLIB$$7f24d4a9.invoke(<generated>)

3

给current_session_context_class设值:

<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate3.SpringSessionContext</prop>  

 报下面的错误...

 Could not execute action: /modules/file_gxb/createFile/checkUnCompletedFile

java.lang.NoSuchMethodException: com.sun.proxy.$Proxy43.checkUnCompletedFile()

我去NoSuchMethodException是个什么鬼?

(如果使用声明式事务管理就不会有这个错误)



怎么办?

在spring的配置文件里加上

<aop:config proxy-target-class="true">

</aop:config>









下面是它的解释

因为你对action配置了aop,并且你用的是默认的jdk动态代理。

jdk代理只能针对接口创建代理,他创建出来的对象只有你实现的接口里面的方法,也就没有你在action里面写的get或者insert之类的方法,运行起来自然会报NoSuchMethodError

加了<aop:config proxy-target-class="true">会使用cglib创建代理,他直接创建目标对象的子类对象,你在action写的那些方法被代理子类对象继承下来了,所以不会报NoSuchMethodException了

感谢panhaichun大神,具体资料见http://bbs.csdn.net/topics/380133183

那么我们就还有另一个办法,就是让我们的bean不要继承接口。







参考资料:

http://bbs.csdn.net/topics/380133183

http://blog.csdn.net/z69183787/article/details/8768421

spring2.5与hibernate3升级后的bug的更多相关文章

  1. macOS 升级后导致 dart bug

    macOS 升级后导致 dart bug macOS 10.15.5 $ demo_app git:(master) flutter doctor # /Users/xgqfrms-mbp/Docum ...

  2. centos 7 升级后yum install出现Exiting on user cancel

    centos 7 升级后yum install出现Exiting on user cancel centos 7.x升级后用yum install进行安装时经常出现Exiting on user ca ...

  3. FastAdmin 升级后出现 is already in use

    FastAdmin 升级后出现 is already in use 升级 FastAdmin 改进很多,但全新安装出现以下错误 Cannot use app\common\library\Menu a ...

  4. Elasticsearch6.2服务器升配后的bug

    .suofang img { max-width: 100% !important; height: auto !important } 本篇文章记录最近一次生产服务器硬件升级之后引起集群不稳定的现象 ...

  5. 关于kali2.0rolling中metasploit升级后无法启动问题的解决总结

    最近在学习metasploit的使用,文中提到可以使用msfupdate命令来对metasploit的payload.exploit等进行升级,我就试了一下,没想到升级过程并不麻烦,但升级后却出现了无 ...

  6. 彻底解决phpcms v9升级后,文章发布出现: Mysql 1267错误:MySQL Error : Illegal mix of collations 解决办法

    彻底解决phpcms v9升级后,文章发布出现: MySQL Query : SELECT * FROM `withli_a`.`v9_keyword` WHERE `keyword` = '吼吼' ...

  7. Android Studio升级后projectBuild failed.

    近期在升级Android Studio后,发现原先能编译通过的project,突然就编译只是了,原因是生成的AndroidManifest.xml文件里有乱码. 升级后: android studio ...

  8. ubuntu12.04升级后找不到共享目录

    备注:采用VMware-workstation 10 更新命令:sudo apt-get update 今天开始搭建Android开发环境,先升级系统,升级后发现windows和ubuntu共享的目录 ...

  9. Ubuntu升级后apache所有的失败,以解决虚拟文件夹的设置

    问题描述: 将Ubuntu离12.04升级到14.04后,出现apache配置的虚拟文件夹所有失效.所有站点域名所有定向到根文件夹.无法分别訪问! 尝试方法: 開始以为是升级后Apache的问题.已经 ...

随机推荐

  1. 天梯赛-L1-018. 大笨钟

    L1-018. 大笨钟 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 微博上有个自称"大笨钟V"的家伙,每 ...

  2. Centos 7安装MYSQL

    1.下载RPM源 直接使用yum命令下载mysql来进行安装是不能成功的,安装过程会有问题,这里需要使用rpm命令来先进下载.下载路径为: http://dev.mysql.com/get/mysql ...

  3. Android Support库——support annotations

    Android Support库是官方出的支持扩展库,包含了丰富的组件.工具类等,通过在Android SDK Manager中勾选以下两项来获取到. 其中,Android Support Libra ...

  4. iOS进阶之页面性能优化

    转载:http://www.jianshu.com/p/1b5cbf155b31 前言 在软件开发领域里经常能听到这样一句话,"过早的优化是万恶之源",不要过早优化或者过度优化.我 ...

  5. MySQL系列教程(四)

    文件打开数(open_files) 我们现在处理MySQL故障时,发现当Open_files大于open_files_limit值时,MySQL数据库就会发生卡住的现象,导致Nginx服务器打不开相应 ...

  6. ubuntu重装指定版本的mysql

    查看错误log cat /var/log/mysql/error.log 首先彻底删除mysql,比如版本5.5 apt-get autoremove --purge mysql-server-5.5 ...

  7. Angular2的input和output(原先的properties和events)

    angular2学习笔记 本文地址:http://blog.csdn.net/sushengmiyan 本文作者:苏生米沿 文章来源:http://blog.ng-book.com/angular-2 ...

  8. hadoop入门级总结一:HDFS

    虽然hadoop经历了多年的发展,作为技术人员都或多或少的使用过或者了解过.这里还是做一个简单的总结,主要原因是之前主要是做hadoop的开发,对hadoop的运维知之甚少,但真正的接触到hadoop ...

  9. Web自动化框架LazyUI使用手册(3)--单个xpath抓取插件详解(selenium元素抓取,有此插件,便再无所求!)

    概述 前面的一篇博文粗略介绍了基于lazyUI的第一个demo,本文将详细描述此工具的设计和使用. 元素获取插件:LazyUI Elements Extractor,作为Chrome插件,用于抓取页面 ...

  10. Android底部导航栏

    Android底部导航栏 今天简单写了一个底部导航栏,封装了一个库,用法比较简单 效果图 Github地址:https://github.com/kongqw/KqwBottomNavigation ...