在使用spring 管理hibernate事物时
spring 中配置:
<!-- 配置申明式的事务管理(基于注解)事务加在父类上 子类也有事务 所以事务一般加载 baseservice 上
-->
<bean id= "transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" >
<property name="sessionFactory" ref="sessionFactory" ></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
但是在测试时 抛出异常:
org.hibernate.HibernateException: save is not valid without active transaction
该异常表示 hibernate 没有拿到事务。
查看hibernate.cfg.xml 中配置:
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
current_session_context_class 表示当前 在调用getcurrentSession 方法是 是冲那里获取事务
表示重线程中获取事务。
百度后 了解 此处配置有问题应该为:
< property name ="current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext </property>
表示 spring 为 当前session 提供事务。