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

xiaoxiao2021-02-28  119

配置文件为这样

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 其他配置: 自动建表 显示sql语句  -->    <property name="hibernate.jdbc.use_scrollable_resultset">false</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> <property name="hibernate.hbm2ddl.auto">update</property> <property name="hibernate.show_sql">true</property> <property name="hibernate.show_sql">true</property>

<property name="connection.autocommit">true</property>

j加上下面这句就会报错/

<property name="hibernate.current_session_context_class">thread</property>

//

<!-- 导入映射文件 --> <mapping resource="cn/hbmy/tas/entity/User.hbm.xml"></mapping> <mapping resource="cn/hbmy/tas/entity/Role.hbm.xml"></mapping>                                      </session-factory>

</hibernate-configuration>

查了一下资料,在单独使用hibernate时,总是会加上这项配置,但spring与hibernate结合时,千万不能加上这句话,我猜测出错原因是事务管理器中的session和当前拿到的session不是同一个session,事务管理器中拿到的session开启了事务,但当前得到的session并没有开启事务,导致出错。所以在整合框架的时候,就不要加上这句话了!!!

转载请注明原文地址: https://www.6miu.com/read-31368.html

最新回复(0)