@Transactional in Spring+Hibernate

Posted by Arun Kumar on Stack Overflow See other posts from Stack Overflow or by Arun Kumar
Published on 2012-10-25T09:54:48Z Indexed on 2012/10/25 11:01 UTC
Read the original article Hit count: 166

Filed under:
|
|

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 Hibernate 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.hibernate3.annotation.AnnotationSessionFactoryBean">
        <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>

      <!--Transaction Manager Added -->  
    <bean id="txManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>

</beans>

Please help.

© Stack Overflow or respective owner

Related posts about spring

Related posts about hibernate