日期:2014-05-18  浏览次数:20634 次

求助 spring 接管hibernate 事物 失败
Exception in thread "main" org.hibernate.HibernateException: createQuery is not valid without active transaction
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:338)
at $Proxy5.createQuery(Unknown Source)
at com.wz.dao.basic.BasicDao.executeQuery(BasicDao.java:39)
at com.wz.test.Test.main(Test.java:22)


applicationContext.xml 配置文件


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>



<bean id="userDaoImpl" class="com.wz.dao.impl.UserDaoImpl">

<property name="sessionFactory" ref="sessionFactory"/>
</bean>
 
 <!-- 配置Hibernate的局部事物管理器 -->
     <bean id="transactionManager"
             class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
         <ref local="sessionFactory" />
        </property>
    </bean>
    
     <!-- 配置事物切面Bean -->
     <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED" />
        </tx:attributes>