开发者

createCriteria requires an active transaction

开发者 https://www.devze.com 2023-02-08 04:56 出处:网络
Im having a problem with some spring + hibernate stuff. <?xml version=\"1.0\" encoding=\"UTF-8\"?>

Im having a problem with some spring + hibernate stuff.

<?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>

error: when calling sessionFactory.getCurrentSession.createCriteria(...开发者_如何学JAVA) i get this:

org.hibernate.HibernateException: createCriteria is not valid without active transaction at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:314) at $Proxy6.createCriteria(Unknown Source) at dao.UserDao.getUsers(UserDao.java:35)

it seems like it is a problem with the transaction manager, but i cannot figure it out


this line=> hibernate.current_session_context_class=thread

seemed to be the culprit. I removed it, and my application worked.


You need to define the transaction manager as below. Here is the complete example

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号