手头有一个项目,使用的是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. WIN2008虚拟机安装ORACLE11g记录

    ---恢复内容开始--- 1.ORACLE11g的安装包下载与解压 官网下载地址:(http://www.oracle.com/technetwork/database/enterprise-edit ...

  2. 单页应用动态设置页面title

    1.适用场景:所有通过router路由的单页应用. 2.示例代码:本文以vue-router为例. 在router.js中: let router = new Router({ routes: [ { ...

  3. Python中enumerate用法详解

    enumerate()是python的内置函数.适用于python2.x和python3.xenumerate在字典上是枚举.列举的意思enumerate参数为可遍历/可迭代的对象(如列表.字符串)e ...

  4. CRM客户关系管理系统(六)

    第六章.排序和搜索功能开发  6.1.排序功能开发 (1)kingadmin_tags.py @register.simple_tag def get_sorted_column(column,sor ...

  5. gdb不知为何显示2次析构

    gdb不知为何显示2次析构 (金庆的专栏 2016.11) gdb 显示2次 A::~A(): (gdb) bt #0 A::~A (this=0x602010, __in_chrg=<opti ...

  6. Kafka系列之-Kafka监控工具KafkaOffsetMonitor配置及使用

    KafkaOffsetMonitor是一个可以用于监控Kafka的Topic及Consumer消费状况的工具,其配置和使用特别的方便.源项目Github地址为:https://github.com/q ...

  7. Visual Studio 写自己的动态链接库(DLL)

    有些时候,我们想写自己的函数库以避免重复写代码,此文介绍如何使用Visual Studio编写自己的动态链接库. 0,实验环境说明: 集成开发环境:Visual Studio 10.0 操作系统: W ...

  8. Linux中的高级文本处理命令,cut命令,sed命令,awk命令

    1.2.1 cut命令 cut命令可以从一个文本文件或者文本流中提取文本列. cut语法 [root@www ~]# cut -d'分隔字符' -f fields    ## 用于有特定分隔字符 [r ...

  9. Android 高级控件(七)——RecyclerView的方方面面

    Android 高级控件(七)--RecyclerView的方方面面 RecyclerView出来很长时间了,相信大家都已经比较了解了,这里我把知识梳理一下,其实你把他看成一个升级版的ListView ...

  10. 有n个数,输出其中所有和为s的k个数的组合。

    分析:此题有两个坑,一是这里的n个数是任意给定的,不一定是:1,2,3...n,所以可能有重复的数(如果有重复的数怎么处理?):二是不要求你输出所有和为s的全部组合,而只要求输出和为s的k个数的组合. ...