在进行使用注解来配置Spring和Hibernate的整合的时候, 遇到了这个问题, 它的意思是说在调用'get'方法的时候,没有活动的事务.

原因分析: Hibernate强制要求在进行数据库操作的时候, 都必须包含在事务当中. 这个时候因为没有事务的管理, 因此报了这个错误.

解决办法: 当然是为这个方法开启事务, 开始事务的方法有两种,

1. 使用Transaction transaction = session.beginTransaction(), 或者是

   /**
* 查询用户
* @param uid
* @return
*/
@Override
public User get(Integer uid) {
Transaction transaction = session.beginTransaction();
User user = null;
try {
user = session.get(User.class,uid);
transaction.commit();
} catch(Exception e) {
e.printStackTrace();
transaction.rollback();
}
return user;
}

2. 使用注解.

但是目前注解加上去没有能够解决问题, 那么就推荐使用前面一种方法, 在调用get方法的前后加上代码

配置事务有点复杂,暂时写到这里, 等待更新

<!-- 事务管理器配置, Hibernate单数据源事务 -->
<bean id="defaultTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean> <!-- 使用annotation定义事务 -->
<tx:annotation-driven transaction-manager="defaultTransactionManager" proxy-target-class="true" />

  

Calling method 'get' is not valid without an active transaction的更多相关文章

  1. Android JS 交互出现 Uncaught Error: Error calling method on NPObject

    由于HTML5的功能越来越强大,native app的一些功能逐步被html页面代替,不可避免的JS交互也用到的也越来越多.在第一个版本向第二个版本迭代的过程中却发生了莫名其妙的问题,第一个版本JS调 ...

  2. spring中使用Hibernate中的getCurrentSession报出:createQuery is not valid without active transaction

    1.错误信息 HTTP Status 500 - createQuery is not valid without active transaction type Exception report m ...

  3. save is not valid without active transaction

    org.hibernate.HibernateException: save is not valid without active transaction at org.hibernate.cont ...

  4. spring整合hibernate的时候报异常org.hibernate.HibernateException: createQuery is not valid without active transaction

    在整合Spring4+hibernate4时候,当代码执行到dao中CRUD操作时,报了一个异常, org.hibernate.HibernateException: createQuery is n ...

  5. 编程异常——假设你报createSQLQuery is not valid without active transaction,...

    非常多时候我们使用hibernate的session时,都是让session在某一执行环境中保持其唯一. 比如在同一线程内用同一个session.在同一方法内用同一session,这样我们就能够用se ...

  6. Exception in thread "main" org.hibernate.HibernateException: save is not valid without active transaction

    在spring4+hibernate4整合过程中,使用@Transactional注解事务会报"Exception in thread "main" org.hibern ...

  7. org.hibernate.HibernateException: getFlushMode is not valid without active transaction

    Spring & Hibernate 整合异常记录: org.hibernate.HibernateException: getFlushMode is not valid without a ...

  8. 如果你报createSQLQuery is not valid without active transaction,请看这里

    原文:https://blog.csdn.net/yinjian520/article/details/8666695 很多时候我们使用hibernate的session时,都是让session在某一 ...

  9. 桥接:JS调用安卓方法报错Uncaught Error: Error calling method on NPObject

    说一说自己粗心踩到的一个不算坑的坑: 项目是安卓webview嵌入SPA单页应用页面,涉及到JS调用原生安卓方法,但就是在调用安卓方法时死活一直报错xxx  NPObject一堆错误.写了一个测试页面 ...

随机推荐

  1. View Programming Guide for iOS ---- iOS 视图编程指南(四)---Views

    Views Because view objects are the main way your application interacts with the user, they have many ...

  2. linux软硬连接学习总结

    创建连接实质上就是给系统中已经存在的文件指定另外一个可以访问它的名称,linux系统当中连接的创建有两种形式:硬链接(Hard Link),与符号链接(Symbloic Link)既软链接. ln命令 ...

  3. C++ STL自学总结,仅供参考

    本文内容,为博主在网上看到资料总结整合而来 一.stl格式简介 .stl文件是在计算机图形应用系统,来表示封闭的面或者体,用来表示三角形网格的一种文件格式.为STereo Lithography的缩写 ...

  4. pl/sql developer中如何导出oracle数据库结构? 参考文章一

    本文作者来自csdn的xieyuooo地址为 : http://bbs.csdn.net/topics/340209135 进入PL/SQL后,使用如下图所示的操作步骤: 然后会弹出一个窗口,在弹出窗 ...

  5. BestCoder Round #74 (div.1) 1002Shortest Path(hdoj5636)

    哈哈哈哈,我就知道这道题目再扔给我,我还是不会,就是这么菜,哈哈哈 一开始官方题解就没搞懂-然后就看了一下别人的代码,水水过就算了.今天拿到-GG: 题意: 一开始,有一张原图,有一条长度为n的链. ...

  6. HDOJ1584蜘蛛牌【DFS】

    10张牌,大的只能跟小的跑,可以针对每一个状态进行搜索,求一个最小的移动距离. 但是不会怎么遍历整个状态是硬伤? 因为只能大的跟着小的. 先把小的标记,去寻找大的点,最终一定是满足的吧. 比如先标记1 ...

  7. python __builtins__ bytes类 (8)

    8.'bytes', 字符串转换成字节流.第一个传入参数是要转换的字符串,第二个参数按什么编码转换为字节. class bytes(object) | bytes(iterable_of_ints) ...

  8. P5024 保卫王国

    传送门 我现在还是不明白为什么NOIPd2t3会是一道动态dp-- 首先关于动态dp可以看这里 然后这里就是把把矩阵给改一改,改成这个形式\[\left[dp_{i-1,0},dp_{i-1,1}\r ...

  9. WPS Office 2019 for Linux来了

    难得啊,焕然一新. WPS Office 2019 For Linux更新说明 11.1.0.8392 版本主要更新: 修复wpsoffice进程存在时不能关机的问题 修复WPS文字模块web版式下拖 ...

  10. Hexo瞎折腾系列(1) - 准备工作与简单美化

    前言 网上有不少相关的帖子,不过版本会比较旧,而不同版本可能存在代码不同的问题,不过大部分还是大同小异,本系列就不啰嗦重复了,基本只会按照本人所使用的版本以及个人所使用到的内容来进行介绍. 该系列是对 ...