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

简介:   Exception in thread "main" org.hibernate.HibernateException: save is not valid without active transaction at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrap


 

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

at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:341)

at $Proxy16.save(Unknown Source)

 

配置文件如下:

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.        xmlns:context="http://www.springframework.org/schema/context"  
  6.        xmlns:aop="http://www.springframework.org/schema/aop"  
  7.        xmlns:tx="http://www.springframework.org/schema/tx"  
  8.        xsi:schemaLocation="http://www.springframework.org/schema/beans   
  9.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  10.            http://www.springframework.org/schema/context  
  11.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  12.            http://www.springframework.org/schema/aop  
  13.            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  14.            http://www.springframework.org/schema/tx  
  15.            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" >  
  16.     <!-- Creates the registry of flow definitions for this application -->  
  17.       
  18.     <!-- 要使用注解必须添加此项 -->  
  19.     <context:annotation-config />  
  20.       
  21.     <!-- 自动检测组件 
  22.      -->  
  23.     <context:component-scan base-package="com.spring.service.impl,com.spring.dao,com.spring.aop" />  
  24.   
  25.       
  26.     <!-- 配置数据源 -->  
  27.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">  
  28.         <property name="driverClassName" value="com.mysql.jdbc.Driver" />  
  29.         <property name="url" value="jdbc:mysql://localhost:3306/test" />  
  30.         <property name="username" value="root" />  
  31.         <property name="password" value="root" />  
  32.     </bean>  
  33.       
  34.     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">  
  35.         <property name="dataSource" ref="dataSource"></property>  
  36.                   
  37.         <!-- 扫描实体包 -->  
  38.         <property name="packagesToScan">  
  39.             <value>com.spring.domain</value>  
  40.         </property>  
  41.           
  42.         <property name="hibernateProperties">  
  43.             <props>  
  44.                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>  
  45.                 <prop key="hibernate.show_sql">true</prop>  
  46.                   
  47.                 <!--   
  48.                     加上下面这句话会出错   
  49.                     Exception in thread "main" org.hibernate.HibernateException: save is not valid without active transaction  
  50.                 -->  
  51.                 <!--<prop key="hibernate.current_session_context_class">thread</prop> --> 
  52.                 <prop key="hibernate.hbm2ddl.auto">update</prop>  
  53.             </props>  
  54.         </property>  
  55.     </bean>  
  56.       
  57.     <!-- 设定transactionManager -->  
  58.      <bean id="txManager"  
  59.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  60.         <property name="sessionFactory" ref="sessionFactory" />  
  61.      </bean>  
  62.   
  63.     <!--启动spring事务注解功能-->  
  64.     <tx:annotation-driven transaction-manager="txManager"/>  
  65.       
  66.       
  67. </beans>  
 

 

正如配置文件中红色文字所示,在单独使用hibernate时,总是会加上这项配置,但spring与hibernate结合时,千万不能加上这句话,我猜测出错原因是事务管理器中的session和当前拿到的session不是同一个session,事务管理器中拿到的session开启了事务,但当前得到的session并没有开启事务,导致出错

在ssh2中的sessionFactory配置文件中应将hibernate.current_session_context_class设为org.springframework.orm.hibernate3.SpringSessionContext(默认为此值),并应用spring管理事务。

如果为<prop key="hibernate.current_session_context_class">thread</prop> 则会报异常,


目录
相关文章
|
Java 数据库连接 数据库
【hibernate 执行方法未插入数据库】hibernate的save方法成功执行,但是未插入到数据库
今天做项目,碰上这个问题: hibernate的save方法成功执行,但是未插入到数据库。 Dao层代码: 1 @Override 2 public void save(T t) { 3 this.
1081 0
|
Java 数据库连接 数据库
Hibernate save, saveOrUpdate, persist, merge, update 区别
Hibernate Save hibernate save()方法能够保存实体到数据库,正如方法名称save这个单词所表明的意思。我们能够在事务之外调用这个方法,这也是我不喜欢使用这个方法保存数据的原因。
1300 0
|
Java 数据库连接 Spring
spring整合hibernate的时候报异常org.hibernate.HibernateException: createQuery is not valid without active transaction
  在整合Spring4+hibernate4时候,当代码执行到dao中CRUD操作时,报了一个异常, org.hibernate.HibernateException: createQuery is not valid without active transaction .
1016 0
|
Java 数据库连接 Spring
No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional
      ssh整合时,使用this.getSessionFactory().getCurrentSession(),会报No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional异常。。 &lt;bean id="sessionFactor
1631 0
|
Spring Java 数据库连接
分析 org.hibernate.HibernateException: No Session found for current thread
/**      *      * org.hibernate.HibernateException: No Session found for current thread      * 分析:getCurrentSession()和当前事务有关系  ...
1017 0
|
安全 Java 应用服务中间件
Java 近期新闻:Hibernate 6.0、JobRunr 5.0、JHipster 7.8.0、Spring CVEs、JReleaser 1.0-RC2
本期 Java 近期新闻综述内容涉及 JDK 19、Spring Boot、Spring CVEs、Apache Tomcat 点版本、Quarkus Tools for Visual Studio Code、Micronaut 3.4.1、JetBrains 加入 Micronaut 基金会、Open Liberty Paketo Liberty Buildpack、Hibernate 6.0、JobRunr 5.0、WildFly 26.1 Beta S2I 镜像、JReleaser 1.0-RC2、MicroStream 7.0-M2、JHipster 7.8.0、JMH 1.35。
782 0
|
19天前
|
SQL Java 数据库连接
jpa、hibernate、spring-data-jpa、jdbcTemplate
jpa、hibernate、spring-data-jpa、jdbcTemplate
|
4月前
|
存储 Java 数据库连接
Spring Boot 嵌入式服务器、Hibernate 关系和 Spring Data 全解析
Spring Boot 的嵌入式服务器功能是一项方便而强大的功能,它允许你在应用程序中直接运行 Web 服务器,无需将其部署到单独的独立 Web 服务器中。这使得开发、测试和部署 Web 应用程序变得容易,而且它还是轻量级的、易于启动和停止的,易于配置。
67 0