title: Why can't I make this bean @Transactional in Spring?
body: <p>I'm writing a simple bean that I want to configure with a table name, an XML file with some data, so that if at application startup the table is empty it get initialized with that data. I decided to use simple SQL queries, but I can't get a session from the sessionfactory, because it says: </p>
  
  <blockquote>
    <p><code>Error creating bean with name 'vecchiOrdiniFiller' defined in ServletContext resource [/WEB-INF/spring/servlet-context.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here</code></p>
  </blockquote>
  
  <p>But this is the configuration (very similar to a service):</p>
  
  
  
  <pre class="lang-xml prettyprint-override"><code>&lt;bean id="transactionManager"
      class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory" ref="mySessionFactory" /&gt;
  &lt;/bean&gt;
  
  &lt;tx:annotation-driven /&gt;
  
  &lt;bean id="ordiniVecchioSistemaLoader" class="it.jsoftware.jacciseweb.assistenza.common.ExcelXmlDataLoader"&gt;
      &lt;property name="xmlFileName" value="WEB-INF/data/daticlientijaccisemarco.xml"&gt;&lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="vecchiOrdiniFiller" class="it.jsoftware.jacciseweb.assistenza.common.BaseTableFiller" init-method="init"&gt;
      &lt;property name = "sessionFactory" ref = "mySessionFactory"&gt;&lt;/property&gt;
      &lt;property name="loader" ref="ordiniVecchioSistemaLoader"&gt;&lt;/property&gt;    
      &lt;property name="tableCreationString" value="CREATE TABLE `vecchiordini` (  `ID` INT(11) NOT NULL AUTO_INCREMENT,  `codicejazz` VARCHAR(255) DEFAULT NULL,  `progressivolicenza` INT(11),  `codicearticolo` VARCHAR(255) DEFAULT NULL,  `rivenditore` VARCHAR(255) DEFAULT NULL,  `cliente` VARCHAR(255) DEFAULT NULL,  PRIMARY KEY (`ID`)) ENGINE=INNODB DEFAULT CHARSET=utf8"&gt;&lt;/property&gt;
      &lt;property name="table" value="vecchiordini"&gt;&lt;/property&gt;
      &lt;property name="tableColumns"&gt;
          &lt;list&gt;
              &lt;value&gt;codicejazz&lt;/value&gt;
              &lt;value&gt;progressivolicenza&lt;/value&gt;
              &lt;value&gt;codicearticolo&lt;/value&gt;
              &lt;value&gt;rivenditore&lt;/value&gt;
              &lt;value&gt;nomecliente&lt;/value&gt;
          &lt;/list&gt;
      &lt;/property&gt;
      &lt;property name="loaderColumns"&gt;
          &lt;list&gt;
              &lt;value&gt;clicod&lt;/value&gt;
              &lt;value&gt;licsmatricola&lt;/value&gt;
              &lt;value&gt;artcod&lt;/value&gt;
              &lt;value&gt;rivenditore&lt;/value&gt;
              &lt;value&gt;cliente&lt;/value&gt;
          &lt;/list&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>and I annotated the init() method with @Transactional. But it doesn't start a transaction and I get that error:</p>
  
  
  
  <pre class="lang-java prettyprint-override"><code>@Transactional
  public void init() throws Exception {
      logger.info("BaseTableFilter per tabella: " + table + ", usando: "
      + loader.getSourceName());
  
      Session session = dao.getSession();
      Transaction tx = session.beginTransaction();
      ...
  </code></pre>
  
  <p>why doesn't that work?</p>