title: spring, hibernate and declarative transaction implementation: there is no active transaction
body: <p>i try to make declarative transaction work.</p>
  
  <p>This is my spring.xml 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:tx="http://www.springframework.org/schema/tx"
         xmlns:context="http://www.springframework.org/schema/context"
  
         xsi:schemaLocation=
         "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt;
  
      &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
          &lt;property name="url" value="jdbc:h2:tcp://my/db/path" /&gt;
          &lt;property name="username" value="username" /&gt;
          &lt;property name="password" value="password" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
          &lt;property name="packagesToScan" value="data" /&gt;
          &lt;property name="hibernateProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.H2Dialect&lt;/prop&gt;
                  &lt;prop key="hibernate.current_session_context_class"&gt;thread&lt;/prop&gt;
                  &lt;prop key="hibernate.transaction.factory_class"&gt;org.hibernate.transaction.JDBCTransactionFactory&lt;/prop&gt;
                  &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
                  &lt;prop key="hibernate.hbm2ddl.auto"&gt;update&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;context:component-scan base-package="test" /&gt;
  
      &lt;tx:annotation-driven/&gt;
  
      &lt;bean id="transactionManager"  class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
          &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
      &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>And this is my controller implementation:</p>
  
  <pre><code>//file TestController.java
  public interface TestController {
  
      public List&lt;Test&gt; findAll();
  
  }
  
  //file TestControllerImp.java
  @Controller
  public class TestControllerImp implements TestController{
  
      @Autowired
      private SessionFactory sessionFactory;
  
      /**
       * @return the sessionFactory
       */
      public SessionFactory getSessionFactory() {
          return sessionFactory;
      }
  
      /**
       * @param sessionFactory the sessionFactory to set
       */
      public void setSessionFactory(SessionFactory sessionFactory) {
          this.sessionFactory=sessionFactory;
      }
  
      @Transactional
      public List&lt;Test&gt; findAll() {
          return sessionFactory.getCurrentSession().createQuery("from Test").list();
      }
  
  }
  </code></pre>
  
  <p>Both are inside package called test.</p>
  
  <p>This is my try:</p>
  
  <pre><code>TestController tc=context.getBean(TestController.class);
  List&lt;Test&gt; list=tc.findAll();
  </code></pre>
  
  <p>But this throw an exception:</p>
  
  <blockquote>
    <p>org.hibernate.HibernateException: createQuery is not valid without active transaction</p>
  </blockquote>
  
  <p>Why transactionManager doesn't work? I hope with @Transactional annotation all transactions will be managed by spring framework.
  What can i do?</p>
  
  <p>Thanks all.</p>