title: Configure Spring + Hibernate Transaction Manager
body: <p>I am trying to configure the transaction manager in Spring in order to annotate my service methods with <code>@Transactional</code>. Unfortunately,I have been looking on others solutions ,but I couldn't find one for me.I am using Tomcat 7.The server starts with no errors but when I call the service method it simply does not recognize <code>@Transacional</code>.</p>
  
  <pre><code>public class GenericDaoImplementation&lt;T&gt; 
  {
      private Class&lt;T&gt; entityClass;
  
      public GenericDaoImplementation(Class&lt;T&gt; entityClass) 
      {
          this.entityClass = entityClass;
      }
  
      @Transactional(propagation = Propagation.REQUIRED)
      public T getByID(String id)
      {
          Session session = HibernateUtil.getSessionFactory().getCurrentSession();    
          T obj = (T) session.get(entityClass, id);
  
          return obj;
      }            
  }
  </code></pre>
  
  <p><code>getByID(String id)</code> is the called method.It has no problem retrieving the data from database if I start a transaction manually.When I annotate it with <code>@Transactional</code> it throws this error :</p>
  
  <pre><code>org.hibernate.HibernateException: get is not valid without active transaction
  </code></pre>
  
  <h2>applicationContext.xml</h2>
  
  <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:context="http://www.springframework.org/schema/context"
          xmlns:tx="http://www.springframework.org/schema/tx"
          xmlns:aop="http://www.springframework.org/schema/aop"
          xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
                              http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
                              http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"&gt;
  
      &lt;context:component-scan base-package="dk.accunu" /&gt;
  
      &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt;
  
      &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
          &lt;property name="driverClassName" value="com.mysql.jdbc.Driver"/&gt;
          &lt;property name="url" value="jdbc:mysql://localhost:3306/test"/&gt;
          &lt;property name="username" value="root"/&gt;
          &lt;property name="password" value=""/&gt;
      &lt;/bean&gt;
  
      &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
           &lt;property name="packagesToScan"&gt;
              &lt;list&gt;
                  &lt;value&gt;dk.accunu.model&lt;/value&gt;
              &lt;/list&gt;
          &lt;/property&gt;
          &lt;property name="configLocation" value="classpath:hibernate.cfg.xml" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
          &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
      &lt;/bean&gt;
  
  
  &lt;/beans&gt;
  </code></pre>
  
  <h2>hibernate.cfg.xml</h2>
  
  <p>
  </p>
  
  <p></p>
  
  <p></p>
  
  <pre><code>&lt;property name="connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt;
  &lt;property name="connection.url"&gt;jdbc:mysql://localhost:3306/test&lt;/property&gt;
  &lt;property name="connection.username"&gt;root&lt;/property&gt;
  &lt;property name="connection.password"&gt;&lt;/property&gt;
   &lt;property name="connection.pool_size"&gt;1&lt;/property&gt;
  
  &lt;!-- SQL dialect --&gt;
  &lt;property name="dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;
  
  &lt;!-- Enable Hibernate's automatic session context management --&gt;
  &lt;property name="current_session_context_class"&gt;thread&lt;/property&gt;
  
  &lt;!-- Disable the second-level cache --&gt;
  &lt;property name="cache.provider_class"&gt;org.hibernate.cache.NoCacheProvider&lt;/property&gt;
  
  &lt;!-- Display all generated SQL to stdout --&gt;
  &lt;property name="show_sql"&gt;true&lt;/property&gt;
  
  &lt;!-- Drop and re-create the database schema on startup --&gt;
  &lt;property name="hbm2ddl.auto"&gt;update&lt;/property&gt;
  
  &lt;mapping class="dk.accunu.model.entities.User" /&gt;
  
  &lt;/session-factory&gt;
  </code></pre>
  
  <p></p>
  
  <p>Please I need some advices.I think it is a configuration problem and I have been trying to solve it for some time already.</p>