title: org.hibernate.HibernateException: No Session found for current thread with tx:advise
body: <p>I am using spring 3.2 with hibernate 4. I want to use spring to control the transactions.
  However with the configuration mentioned below I get the </p>
  
  <p>'Servlet.service() for servlet spring threw exception: org.hibernate.HibernateException: No Session found for current thread' </p>
  
  <p>exception:</p>
  
  <pre><code>&lt;aop:config&gt;
      &lt;aop:pointcut id="serviceMethods"
          expression="execution(* com.locator.service.impl.ServiceTypeService.*(..))" /&gt;
      &lt;aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods" /&gt;
  &lt;/aop:config&gt;
  
  &lt;tx:advice id="txAdvice" transaction-manager="hbTransactionManager"&gt;
      &lt;tx:attributes&gt;
          &lt;tx:method name="*" propagation="REQUIRED" /&gt;
      &lt;/tx:attributes&gt;
  &lt;/tx:advice&gt;
  
  &lt;!-- Hibernate session factory --&gt;
  &lt;bean id="hbSessionFactory"
      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
      &lt;property name="dataSource"&gt;
          &lt;ref bean="dataSource" /&gt;
      &lt;/property&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;property name="mappingResources"&gt;
          &lt;list&gt;
              &lt;value&gt;../spring/model/ServiceType.hbm.xml&lt;/value&gt;
          &lt;/list&gt;
      &lt;/property&gt;
  
  &lt;/bean&gt;
  
  &lt;bean id="hbTransactionManager"
      class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory" ref="hbSessionFactory" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="serviceTypeService" class="com.locator.service.impl.ServiceTypeService"&gt;
      &lt;property name="serviceTypeDao" ref="serviceTypeDao"&gt;&lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="serviceTypeDao" class="com.locator.dao.impl.ServiceTypeDao"&gt;
      &lt;property name="sessionFactory" ref="hbSessionFactory"&gt;&lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>The code for the Dao layer and the Service is as follows:</p>
  
  <pre><code>public class ServiceTypeDao implements IServiceTypeDao{
  
      private static final Log log = LogFactory.getLog(ServiceTypeDao.class);
  
      private SessionFactory sessionFactory;
  
      public SessionFactory getSessionFactory(){
          return sessionFactory;
      }
  
      public void setSessionFactory(SessionFactory sessionFactory){
          this.sessionFactory = sessionFactory;
      }
  
      public ServiceType findById(int id) {
          log.debug("getting ServiceType instance with id: " + id);
          try {
              Session session = getSessionFactory().getCurrentSession();
              ServiceType instance = (ServiceType)  session.get("com.locator.model.ServiceType", id);
              if (instance == null) {
                  log.debug("get successful, no instance found");
              } else {
                  log.debug("get successful, instance found");
              }
  
              instance.setName(instance.getName()+"0");
              session.saveOrUpdate(instance);
  
              return instance;
          }catch (RuntimeException re) {
              log.error("get failed", re);
              throw re;
          }
      }
  }
  
  
  public class ServiceTypeService implements IServiceTypeService{
  
      private ServiceTypeDao serviceTypeDao;
  
      public void setServiceTypeDao(ServiceTypeDao serviceTypeDao){
          this.serviceTypeDao = serviceTypeDao;
      }
  
      public ServiceType getServiceTypeById(int id){
          return serviceTypeDao.findById(id);
      }
  }
  </code></pre>
  
  <p>Replacing getSessionFactory().getCurrentSession() with getSessionFactory().openSession() will resolve the above issue however, it will mean that the developer will then be responsible for the session open/close rather than spring. Therefore, please advise how this can be resolved using spring.  </p>