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

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

问题描述

我在我的web应用程序中使用 Spring 3.1 + Hibernate 4.x
在我的DAO中,我保存了用户类型对象,如下所示:

  sessionFactory.getCurrentSession()。save(user) ; 

但是得到以下异常:

<$ p $如果没有活动事务,则保存无效

我搜索了一下,发现类似的关于SO的问题 ,使用以下解决方案:

 会话会话= getSessionFactory()。getCurrentSession(); 
Transaction trans = session.beginTransaction();
session.save(entity);
trans.commit();

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



无法直接使用 sessionFactory.getCurrentSession()。save(user); 事务手动吗?



我也尝试在我的service / dao方法上使用 @Transactional ,但问题依然存在。



编辑:下面是我的Spring配置文件:

 <?xml version =1.0encoding =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>


<! - 启用基于注释的事务行为配置 - >
< tx:annotation-driven transaction-manager =txManager/>

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 =dataSourceref =dataSource/>
< property name =packagesToScanvalue =com.myapp.entities/>
< property name =hibernateProperties>
<道具>
< prop key =hibernate.dialect> org.hibernate.dialect.MySQLDialect< / prop>
< prop key =hibernate.show_sql> true< / prop>
< /道具>
< / property>
< / bean>


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

< / beans>

我使用以下Hibernate 4依赖项:

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

< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-entitymanager< / artifactId>
< version> 4.1.1.Final< / version>
< /依赖关系>

< dependency>
< groupId> cglib< / groupId>
< artifactId> cglib< / artifactId>
< version> $ {cglib.version}< / version>
< scope>运行时< / scope>
< /依赖关系>

请协助。

解决方案

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

 < 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属性就可以了。

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);

But getting following exception:

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

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.

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

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

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>

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>

Please help.

解决方案

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>

i think it should be

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

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

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

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