手头有一个项目,使用的是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. .net通用CMS快速开发框架——问题1:Dapper通用的多表联合分页查询怎么破?

    最近在弄一个东东,类似那种CMS的后台管理系统,方便作为其它项目的初始化框架用的. 现在遇到个问题,如标题所示:Dapper通用的多表联合分页查询怎么破? 难道只能通过拼接sql或者使用存储过程吗?我 ...

  2. Python中求1到20平方的两种方法

    #1.使用列表推导式 >>> [x**2 for x in range(1,21)] [1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, ...

  3. DotnetSpider (二) Downloader的设置 Request自定义数据字典

    本篇主要分享自定义Downloader和Request信息,实现自定义请求内容,及将自定义内容存储. ** 温馨提示:如需转载本文,请注明内容出处.**     本文连接:http://www.cnb ...

  4. asp.net使用session完成: 从哪个页面进入登录页面,登录成功还回到那个页面

    1.在Login.aspx页面Load中加入 if (!IsPostBack && Request.UrlReferrer != null) {      Session[ " ...

  5. 计算机网络之IP协议族

    网际协议IP 与IP协议配套使用的还有三个协议: 地址解析协议 ARP   (Address Resolution Protocol) 网际控制报文协议 ICMP  (Internet Control ...

  6. 自定义View实现五子棋游戏

    成功的路上一点也不拥挤,因为坚持的人太少了. ---简书上看到的一句话 未来请假三天顺带加上十一回家结婚,不得不说真是太坑了,去年婚假还有10天,今年一下子缩水到了3天,只能赶着十一办事了. 最近还在 ...

  7. How to Collect Bne Log Files for GL Integrators

    In this Document   Goal   Solution APPLIES TO: Oracle General Ledger - Version 11.0 and laterInforma ...

  8. linux中probe函数传递参数的寻找(下)

    点击打开链接 linux中probe函数传递参数的寻找(下) 通过追寻driver的脚步,我们有了努力的方向:只有找到spi_bus_type的填充device即可,下面该从device去打通,当两个 ...

  9. ROS(indigo)ROSPlan框架

    源码地址:https://github.com/KCL-Planning/ROSPlan/wiki ROSPlan框架 ROSPlan框架提供了用于在ROS的系统任务规划的通用方法.ROSPlan的两 ...

  10. Python 3 函数自由变量的大坑

    Python中函数是一个对象, 和整数,字符串等对象有很多相似之处,例如可以作为其他函数的参数或返回对象, Python中的函数还可以携带自由变量, 两者无疑极大增进了Python的表达力. 但是Py ...