title: @Transactional annotation required due to 'org.hibernate.HibernateException: No Session found for current thread' exception
body: <p>So, I read that the best place to put the @Transactional annotation was outside the DAO classes which contains the db access methods, like in a service class which use those methods.</p>
  
  <p>Now, the problem is, once I've already remove this annotations from the DAO classes, I launch the DAO test methods and the aforementioned exception raised. I put back the annotations in the DAO classes and this exception doesn't raises anymore.</p>
  
  <p>Then my question is: how can I clear my DAOs of this annotations and still have my tests working?</p>
  
  <p>Let's add some code:</p>
  
  <p><strong>DAO class</strong></p>
  
  <pre><code>public class UserDAO  extends IDAO implements IUserDAO {
  
  
      @Override
      //@Transactional(readOnly=true)
      public User get(int idUser) {
          return (User) currentSession().get(User.class,idUser);
      }}
  </code></pre>
  
  <p><strong>IDAO Class</strong></p>
  
  <pre><code>public abstract class IDAO {
  
      protected SessionFactory sessionFactory;
  
      public void setSessionFactory(SessionFactory sessionFactory) {
          this.sessionFactory = sessionFactory;
      }
  
      public Session currentSession()
      {
          return sessionFactory.getCurrentSession();
      }
  
  }
  </code></pre>
  
  <p><strong>Test class</strong></p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = { "classpath:META-INF/spring/app-context.xml" })
  public class UserDAOTest extends AbstractJUnit4SpringContextTests {
  
      @Autowired
      private IUserDAO userDAO;
  
      @Test
      public void testGetUser() throws Exception {
          User user = userDAO.get(2);
          assertNotNull(user);
      }
  }
  </code></pre>
  
  <p><strong>app-config</strong></p>
  
  <pre class="lang-xml prettyprint-override"><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:jdbc="http://www.springframework.org/schema/jdbc"
      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/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"&gt;
  
      &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close"&gt;
          &lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt;
          &lt;property name="url"
              value="jdbc:mysql://localhost:3306/waldb" /&gt;
          &lt;property name="username" value="user" /&gt;
          &lt;property name="password" value="password" /&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="annotatedClasses"&gt;
              &lt;list&gt;
                  &lt;value&gt;com.wal.serverside.persistence.domain.User&lt;/value&gt;
              &lt;/list&gt;
          &lt;/property&gt;
          &lt;property name="hibernateProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQL5Dialect&lt;/prop&gt;
                  &lt;prop key="hibernate.hbm2ddl.auto"&gt;update&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&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;bean id="userDAO" class="com.wal.serverside.persistence.DAO.UserDAO"&gt;
          &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
      &lt;/bean&gt;
  
      &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;
  
      &lt;context:component-scan base-package="com.wal.serverside.persistence" /&gt;
  
  &lt;/beans&gt;
  </code></pre>