title: "HibernateException: No Session found for current thread" when calling DAO from Transactional service method
body: <p>I have a service bean that loads objects from the database after instantiation into an object cache. If I label the service method that calls my DAO object method as @Transactional, then I get the "HibernateException: No Session found for current thread" error. However, if I label the DAO class as @Transactional, I get no such error and it works fine.</p>
  
  <p>The problem is that I can't then make multiple DAO calls from the same method in the service object and have it be one transaction. Are there any thoughts on what might cause this?</p>
  
  <p>I am using Spring 3.1 and Hibernate 4.</p>
  
  <p>DAO Example:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Repository
  public class HibernateObjectDao implements ObjectDao {
  
      SessionFactory sessionFactory;
  
      public void setSessionFactory(SessionFactory sessionFactory) {
          this.sessionFactory = sessionFactory;
      }
  
      public List&lt;Object&gt; getObjects() {
          return sessionFactory.getCurrentSession()
              .createQuery("from Object").list();
      }
  }
  </code></pre>
  
  <p>Service Bean example:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Service
  public class MyServiceBean implements AbstractMyServiceBean
  {
  
  
      @Resource
      private ObjectDao objectDao;
  
      private HashMap&lt;String,Object&gt; objectCache;
  
  
      public MyServiceBean() {
          this.objectCache = new HashMap&lt;String,Object&gt;();
      }
  
  
      @Autowired
      public void setObjectDao(ObjectDao objectDao) {
          this.objectDao = objectDao;
      }
  
  
      @Transactional
      public void initialize() {
          loadObjectCache();
      }
  
      public void loadObjectCache() {
          objectCache.put("stuff",this.objectDao.getObjects())
      }
  }
  </code></pre>
  
  <p>ApplicationContext.xml excerpt:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;bean id="objectDao" class="com.example.persistence.HibernateObjectDao"&gt;
      &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="myServiceBean"
      class="com.example.service.MyServiceBean" 
      init-method="initialize"&gt;
      &lt;property name="objectDao" ref="objectDao" /&gt;
  &lt;/bean&gt;
  </code></pre>