createCriteria需要活动事务

时间:2011-02-02 04:01:15

标签: hibernate spring transactionmanager

我遇到了一些spring + hibernate的问题。

<?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-2.0.xsd
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

<!-- the parent application context definition for the springapp application -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>
<bean id="mySessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="mappingResources">
        <list>
            <value>domain/User.hbm.xml</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <value>
            hibernate.dialect=org.hibernate.dialect.HSQLDialect
            hibernate.current_session_context_class=thread
        </value>
    </property>
</bean>

 <tx:annotation-driven transaction-manager="transactionManager"/>

<bean id="transactionManager" 
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="mySessionFactory"/>
</bean>


<bean id="userDao" class="dao.UserDao">
    <property name="factory" ref="mySessionFactory" />
</bean>

<bean id="userManager" class="service.SimpleUserManager">
    <property name="userDao" ref="userDao" />
</bean>


<bean id="locationDao" class="dao.LocationDao">
    <property name="factory" ref="mySessionFactory" />
</bean>


<bean id="locationManager" class="service.SimpleLocationManager">
    <property name="locationDao" ref="locationDao" />
</bean>




<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:jdbc.properties</value>
        </list>
    </property>
</bean>

错误:调用sessionFactory.getCurrentSession.createCriteria(...)时我得到了这个:

org.hibernate.HibernateException:没有活动事务,createCriteria无效     at org.hibernate.context.ThreadLocalSessionContext $ TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:314)     在$ Proxy6.createCriteria(未知来源)     在dao.UserDao.getUsers(UserDao.java:35)

它似乎是事务管理器的问题,但我无法弄明白

2 个答案:

答案 0 :(得分:3)

此行=&gt;的hibernate.current_session_context_class =螺纹

似乎是罪魁祸首。我删除它,我的应用程序工作。

答案 1 :(得分:1)

您需要定义事务管理器,如下所示。 Here is the complete example

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="mySessionFactory" />
 </bean>