Spring+Hibernate 中的@Transactional [英] @Transactional in Spring+Hibernate

查看:11
本文介绍了Spring+Hibernate 中的@Transactional的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 Web 应用程序中使用 Spring 3.1 + Hibernate 4.x.在我的 DAO 中,我将用户类型对象保存如下

I an using Spring 3.1 + Hibernate 4.x in my web application. In my DAO, i am saving User type object as following

sessionFactory.getCurrentSession().save(user);

但得到以下异常:

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

我用谷歌搜索并发现了类似的问题 在 SO 上,使用以下解决方案:

I googled and found similar question on SO, with following solution:

   Session session=getSessionFactory().getCurrentSession();
   Transaction trans=session.beginTransaction();
   session.save(entity);
   trans.commit();

这就解决了问题.但是在那个解决方案中,手动开始和提交事务有很多混乱.

That solves the problem. But in that solution, there is lot of mess of beginning and committing the transactions manually.

我不能直接使用 sessionFactory.getCurrentSession().save(user); 而无需手动开始/提交事务吗?

Can't i use sessionFactory.getCurrentSession().save(user); directly without begin/commit of transactions manually?

我也尝试在我的服务/dao 方法上使用 @Transactional,但问题仍然存在.

I try to use @Transactional on my service/dao methods too, but the problem persists.

这是我的 Spring 配置文件:

EDIT : Here is my Spring Config File:

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


  <!-- enable the configuration of transactional behavior based on annotations -->
  <tx:annotation-driven transaction-manager="txManager"/>

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource"
        p:driverClassName="${db.driverClassName}" p:url="${db.url}"
        p:username="${db.username}" p:password="${db.password}" />

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.myapp.entities" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>


    <bean id="txManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>

</beans>

我正在使用以下 Hibernate 4 依赖项:

I am using following Hibernate 4 dependencies:

<!-- Hibernate Dependency -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.1.7.Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.1.1.Final</version>
        </dependency>

        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>${cglib.version}</version>
            <scope>runtime</scope>
        </dependency>

请帮忙.

推荐答案

我认为您正在使用 Hibernate 4.x 那么为什么您在应用程序上下文文件中使用 Hibernate 3 事务管理器?

i think you are using Hibernate 4.x then why you are using hibernate 3 transaction manager in application context file?

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

我觉得应该是

<bean id="txManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory">
        <ref bean="sessionFactory" />
    </property>
</bean>

尝试使用 hibernate 4 事务管理器以及它应该可以工作的 @Transactional 属性.

just try to use hibernate 4 transaction manager along with @Transactional attribute it should work.

这篇关于Spring+Hibernate 中的@Transactional的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆