title: @Transactional in controller method not working with Hibernate Session
body: <p>I have developed a webapp using Spring MVC + Hibernate, and using three layers, Controller layer, Service layer and Dao layer.</p>
  
  <p>Now I want to provide a REST api for my webapp.</p>
  
  <p>Since I have a GenericDao which provides generic methods like find(id), findAll(), findByProperty() I thought I could skip the Service Layer in the Api Controller and inject the daos into the Controller itself, otherwise I would have to create class-specific methods for each of my domain objects for these generic find, findAll methods, which is a pain in the ass when I want to presetn just raw data. </p>
  
  <p>My first, more generic question is regarding this architecture decision. Is this a good solution?</p>
  
  <p>My second (and main) question is that I am having trouble when annotating my Controller method with <code>@Transactional</code> so a hibernate session is opened. It seems that it's not working at all.</p>
  
  <p>I even creted an interface as said in <a href="https://stackoverflow.com/questions/12971848/making-spring-3-mvc-controller-method-transactional">this question</a>.</p>
  
  <p>IApiController</p>
  
  <pre><code>@Controller
  public interface IApiController {
  
      @ResponseBody
      public String getStation(Long id);
      @ResponseBody
      public String getStations();
  
  
  }
  </code></pre>
  
  <p>ApiController</p>
  
  <pre><code>@Controller
  @RequestMapping("/api")
  public class ApiController extends BaseApiController implements IApiController {
  
      @Autowired
      private IStationDao stationDao;
  
  
      @RequestMapping(value = "stations/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
      @ResponseBody
      @Transactional(readOnly=true)
      public String getStation(@PathVariable Long id){
          Station station = stationDao.findById(id);
          return pack(station);
      }
  
  
      @Override
      @RequestMapping(value = "stations", produces = MediaType.APPLICATION_JSON_VALUE)
      @ResponseBody
      @Transactional(readOnly=true)
      public String getStations() {
          List&lt;Station&gt; stations = stationDao.findAll();
          return pack(stations);
      }
  
  
  }
  </code></pre>
  
  <p>When I call <code>api/stations</code> I get a <code>HibernateException: No Session found for current thread</code></p>
  
  <p>Context config</p>
  
  <pre><code>&lt;context:component-scan base-package="my controller package" /&gt;
      &lt;mvc:annotation-driven /&gt;
      &lt;context:annotation-config /&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="SessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
          &lt;property name="configLocation"&gt;
              &lt;value&gt;classpath:hibernate.cfg.xml&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="mappingLocations"&gt;
              &lt;list&gt;
                  &lt;value&gt;classpath*:/hbm/*.hbm.xml&lt;/value&gt;
              &lt;/list&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  </code></pre>