title: @Transactional in Spring+Hibernate
body: <p>I an using <strong>Spring 3.1 + Hibernate 4.x</strong> in my web application. 
  In my DAO, i am saving User type object as following </p>
  
  <pre><code>sessionFactory.getCurrentSession().save(user);
  </code></pre>
  
  <p>But getting following exception:</p>
  
  <pre><code>org.hibernate.HibernateException: save is not valid without active transaction
  </code></pre>
  
  <p>I googled and found similar <a href="https://stackoverflow.com/questions/12002737/org-hibernate-hibernateexception-save-is-not-valid-without-active-transaction">question</a> on SO, with following solution:</p>
  
  <pre><code>   Session session=getSessionFactory().getCurrentSession();
     Transaction trans=session.beginTransaction();
     session.save(entity);
     trans.commit();
  </code></pre>
  
  <p>That solves the problem. But in that solution, there is lot of mess of beginning and committing the transactions manually. </p>
  
  <p>Can't i use     <code>sessionFactory.getCurrentSession().save(user);</code> directly without begin/commit of transactions manually?</p>
  
  <p>I try to use <code>@Transactional</code> on my service/dao methods too, but the problem persists.</p>
  
  <p><strong>EDIT :</strong> Here is my Spring Config File:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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"&gt;
  
  
    &lt;!-- enable the configuration of transactional behavior based on annotations --&gt;
    &lt;tx:annotation-driven transaction-manager="txManager"/&gt;
  
      &lt;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}" /&gt;
  
      &lt;bean id="sessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
          &lt;property name="packagesToScan" value="com.myapp.entities" /&gt;
          &lt;property name="hibernateProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/prop&gt;
                  &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
  
      &lt;bean id="txManager"
          class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
          &lt;property name="sessionFactory"&gt;
              &lt;ref bean="sessionFactory" /&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>I am using following Hibernate 4 dependencies:</p>
  
  <pre><code>&lt;!-- Hibernate Dependency --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
              &lt;version&gt;4.1.7.Final&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-entitymanager&lt;/artifactId&gt;
              &lt;version&gt;4.1.1.Final&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;cglib&lt;/groupId&gt;
              &lt;artifactId&gt;cglib&lt;/artifactId&gt;
              &lt;version&gt;${cglib.version}&lt;/version&gt;
              &lt;scope&gt;runtime&lt;/scope&gt;
          &lt;/dependency&gt;
  </code></pre>
  
  <p>Please help.</p>