title: Hibernate cannot find session bound to thread when using @PostConstruct
body: <p>I'm reworking an older Spring project to more reflect how things are supposed to be done in Spring 3.0.x.</p>
  
  <p>One of the changes I made was to the repository/dao layer. As advised by best practices, I no longer extend from <code>HibernateDaoSupport</code> to use the <code>HibernateTemplate</code>, but instead I use the Hibernate <code>SessionFactory</code> directly by using <code>sessionFactory.getCurrentSession()</code>, which is supposed to work with Spring 3.0.x and above.</p>
  
  <p>This has been a very big boon for the project as a whole, as it gets rid of all of the wrapping code caused by <code>HibernateTemplate</code>. However, I just noticed that I can no longer call into Service methods that were using @PostConstruct (or were using the bean's <code>onStartUp</code> attribute in the XML application context)</p>
  
  <p>For example, this method used to work just fine using <code>HibernateTemplate</code>, but now Hibernate throws an exception complaining that there is no session bound to the thread:</p>
  
  <pre><code>@Override
  @PostConstruct
  public void onStartUp() {
      logger.debug("Starting Bootstrap Service...");
  
      createSysAdminUser();
      createDefaultRoles();
      createDefaultThemes();
  
      createStopListIfDoesNotExist();
      stopListService.load();
  
      partialMappingService.load();
      dictionaryService.load();
  }
  </code></pre>
  
  <p>I could just remove this <code>@PostConstruct</code> method call... it's the only one in the system. It is called when the application starts up to bootstrap data for a new application. Most of the time, it does nothing on a production system, but it's handy to have it for test and development databases that were created fresh.</p>
  
  <p>Any ideas as to why and how I can fix it?</p>
  
  <p>Thanks!</p>
  
  <p>EDIT: Here is my transaction manage advice config:</p>
  
  <pre><code>&lt;aop:config&gt;
      &lt;aop:advisor advice-ref="transactionAdvice"
                   pointcut="execution(* *..service.*.*(..))" order="1"/&gt;
      &lt;!-- gets sub packages like service.user --&gt;
      &lt;aop:advisor advice-ref="transactionAdvice"
                   pointcut="execution(* *..service.*.*.*(..))" order="2"/&gt;
  &lt;/aop:config&gt;
  
  &lt;tx:advice id="transactionAdvice" transaction-manager="transactionManager"&gt;
      &lt;tx:attributes&gt;
          &lt;tx:method name="find*" read-only="true" propagation="REQUIRED"/&gt;
          &lt;tx:method name="get*" read-only="true" propagation="REQUIRED"/&gt;
          &lt;tx:method name="*" propagation="REQUIRED"/&gt;
      &lt;/tx:attributes&gt;
  &lt;/tx:advice&gt;
  </code></pre>