在spring4+hibernate4整合过程中,使用@Transactional注解事务会报"Exception in thread "main" org.hibernate.HibernateException: save is not valid without active transaction"错误,具体错误如下:

Exception in thread "main" org.hibernate.HibernateException: save is not valid without active transaction
at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:352)
at $Proxy18.save(Unknown Source)
at com.zainagou.dao.UserDaoImpl.addUser(UserDaoImpl.java:23)
at com.zainagou.dao.UserDaoImpl$$FastClassBySpringCGLIB$$98e1b102.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
at com.zainagou.dao.UserDaoImpl$$EnhancerBySpringCGLIB$$ac7c23a8.addUser(<generated>)
at com.zainagou.dao.MyTest.main(MyTest.java:19)

,后来发现是由于使用了如下配置导致上述问题:

<!-- session绑定当前线程 -->
<prop key="hibernate.current_session_context_class">thread</prop>

改配置在单独使用hibernate时进行当前线程绑定,在spring+hiberante整合中不需要这句配置,

spring提供了一个SpringSessionContext这个类来帮你获得和管理session

Exception in thread "main" org.hibernate.HibernateException: save is not valid without active transaction的更多相关文章

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

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

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

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

  3. 异常:getHibernateFlushMode is not valid without active transaction; nested exception is org.hibernate.HibernateException: getHibernateFlushMode is not valid without active transaction getHibernateFlu

    场景: 在使用spring整合hibernate调用的HibernateTemplate时报错解决: 在spring配置文件中添加事务的配置 <bean id="hibernateTr ...

  4. Exceprtion:e createQuery is not valid without active transaction; nested exception is org.hibernate.HibernateException: createQuery is not valid without active transaction

    如果增加配置了current_session_context_class属性,查询的时候需要session.beginTrasaction()来开启事务

  5. Hibernate 与Spring整合出现 hibernate.HibernateException: createCriteria is not valid without active transaction

    当 Hibernate 和 Spring 整合时,在 Spring 中指定的 Hibernate.cfg.xml 文件内容中要注释掉以下内容: <!-- Enable Hibernate's a ...

  6. save is not valid without active transaction

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

  7. Exception in thread "main" org.hibernate.TransientObjectException: object references an unsaved tran

    今天在使用一对多,多对一保存数据的时候出现了这个错误 Hibernate错误: Exception in thread "main" org.hibernate.Transient ...

  8. ERROR: Field 'PostId' doesn't have a default value Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not execute statement

    例子: Post p = new Post(); p.setPostId(3); p.setPostName("技术"); 在执行数据保持时提示session.save(p); 的 ...

  9. 在运行Hibernate Hello World程序的时候,抛如下错误: view plain Exception in thread "main" org.hibernate.exception.LockAcquisitionException 解决方法

    在运行Hibernate Hello World程序的时候,抛如下错误: Exception in thread "main" org.hibernate.exception.Lo ...

随机推荐

  1. jquery中,使用append增加元素时,该元素的绑定监听事件失效

    举例:如果在一个<div id="resultArea"></div>中,通过append添加一个id="checkOutTip"的文本 ...

  2. Linux下网络流量实时监控工具大全

    在工作中发现,经常因为业务的原因,需要即时了解某台服务器网卡的流量,虽然公司也部署了cacti软件,但cacti是五分钟统计的,没有即时性,并且有时候打开监控页面不方便,个人喜欢随手在某台服务器上输入 ...

  3. mysqlslap 使用总结

    mysqlslap 可以用于模拟服务器的负载,并输出计时信息.其被包含在 MySQL 5.1 的发行包中.测试时,可以指定并发连接数,可以指定 SQL 语句.如果没有指定 SQL 语句,mysqlsl ...

  4. Android之布局大全

    Android布局方式可以归类为ViewGroup的5个类别,即ViewGroup的5个直接子类.其它的一些布局都扩展自这5个类. 上结构图: 转自:http://www.cnblogs.com/de ...

  5. IOS即时通讯XMPP搭建openfire服务器

    一.下载并安装openfire 1.到http://www.igniterealtime.org/downloads/index.jsp下载最新openfire for mac版 比如:Openfir ...

  6. Oracle数据库用户权限和管理员权限

    一.如何查看权限 查看用户权限 1. oracle用户查看自己的权限和角色     select * from user_tab_privs;     select * from user_role_ ...

  7. R语言中的if-else语句写法

    结构  1 :  if()  xx  else    yy    一行: 结构  2:   if()  {xx} else  {yy} 或者   if(){ xx }else    #此处不能两行写 ...

  8. Git 的是使用入门

    Coding 代码管理快速入门 当项目创建好了之后,我们该如何上传代码到 coding 上呢? Coding 网站使用“ Git 仓库”(类似 github )来管理代码. 其操作原理在于:利用 gi ...

  9. apache的工作模式 和 最大连接数设置

    经过测试 效果明显 (1)首选查看apache的工作模式 windows下的查看apache的工作模式命令:httpd -l 如果列出mod_win32.c,则表示是 win32.c 工作方式. 列出 ...

  10. Struts2框架学习(一)

    Struts2框架学习(一) 1,Struts2框架介绍 Struts2框架是MVC流程框架,适合分层开发.框架应用实现不依赖于Servlet,使用大量的拦截器来处理用户请求,属于无侵入式的设计. 2 ...