手头有一个项目,使用的是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. ORACLE设置自启动记录

    设置开机自启动1. 修改Oracle系统配置文件:/etc/oratab,只有这样,Oracle 自带的dbstart和dbshut才能够发挥作用.[root@hailiang ~]# vi /etc ...

  2. JAVA 第二天 内部类

    package com.company; /** * Created by Administrator on 2016/8/23. */ public class Outter {//生成的字节码文件 ...

  3. DotnetSpider (一) 架构的理解、应用、搭建

    第一次写博客,比较浅显,欢迎大牛们指点一二,不胜感激.   ** 温馨提示:如需转载本文,请注明内容出处.**   本文连接:http://www.cnblogs.com/grom/p/8931650 ...

  4. async/await 的一些知识

    博文 Don't Block on Async Code What is the purpose of "return await" in C#? Any difference b ...

  5. A quike guide teaching you how to use matlab to read netCDF file and plot a figure

    1.       Preparation 2.       A brief introduce to netCDF. 4 3.       Data Structure. 4 3.1   Attrib ...

  6. 浅谈JS变量声明和函数声明提升

    先来两个问题 很多时候,在直觉上,我们都会认为JS代码在执行时都是自上而下一行一行执行的,但是实际上,有一种情况会导致这个假设是错误的. a = 2; var a; console.log(a); 按 ...

  7. CSDN没有审核投诉的真实性直接删除博主上传的资源

      今天打开博客,发现一条未读通知:您上传的资源* * * *因质量投诉没有通过审核,如有疑问,请联系webmaster@csdn.net 我马上去看了下我的资源下载页,资源已经被删除,积分也已清空- ...

  8. 初识Spark2.0之Spark SQL

    内存计算平台spark在今年6月份的时候正式发布了spark2.0,相比上一版本的spark1.6版本,在内存优化,数据组织,流计算等方面都做出了较大的改变,同时更加注重基于DataFrame数据组织 ...

  9. PHP Ajax JavaScript Json 实现天气信息获取

    使用第三方服务 间接方式 思路 使用到的服务 实现代码 前端完整代码 总结 要在自己的网站上添加一个天气预报功能,是一个很普通的需求,实现起来也不是很难.今天来介绍几个简单的方法. 使用第三方服务 有 ...

  10. 从源码安装git

    蛋疼的阿里云,git版本居然才1.9.只能手动安装了. 预装 apt-get update apt-get --yes install libcurl4-gnutls-dev libexpat1-de ...