title: Spring / Hibernate / JUnit - No Hibernate Session bound to Thread
body: <p>I'm trying to access the current hibernate session in a test case, and getting the following error:</p>
  
  <blockquote>
    <p>org.hibernate.HibernateException: No
    Hibernate Session bound to thread, and
    configuration does not allow creation
    of non-transactional one here     at
    org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)
      at
    org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:574)</p>
  </blockquote>
  
  <p>I've clearly missed some sort of setup, but not sure what.</p>
  
  <p>Any help would be greatly appreciated.  This is my first crack at Hibernate / Spring etc, and the learning curve is certainly steep!</p>
  
  <p>Code follows:</p>
  
  <p>The offending class:</p>
  
  <pre><code>public class DbUnitUtil extends BaseDALTest {
      @Test
      public void exportDtd() throws Exception
      {
  
          Session session = sessionFactory.getCurrentSession();
  
          session.beginTransaction();
          Connection hsqldbConnection = session.connection(); 
  
          IDatabaseConnection connection = new DatabaseConnection(hsqldbConnection);
  
          // write DTD file
          FlatDtdDataSet.write(connection.createDataSet(), new FileOutputStream("test.dtd"));
      }
  }
  </code></pre>
  
  <p>Base class:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations={"classpath:applicationContext.xml"})
  public class BaseDALTest extends AbstractJUnit4SpringContextTests {
      public BaseDALTest()
      {
          super();
      }
          @Resource
          protected SessionFactory sessionFactory;
  }
  </code></pre>
  
  <p>applicationContext.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"&gt;
  
      &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
          &lt;property name="driverClassName"&gt;
              &lt;value&gt;org.hsqldb.jdbcDriver&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="url"&gt;
              &lt;value&gt;jdbc:hsqldb:mem:sample&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="username"&gt;
              &lt;value&gt;sa&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="password"&gt;
              &lt;value&gt;&lt;/value&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="sessionFactory" class="com.foo.spring.AutoAnnotationSessionFactoryBean"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
          &lt;property name="entityPackages"&gt;
              &lt;list&gt;
                  &lt;value&gt;com.sample.model&lt;/value&gt;
              &lt;/list&gt;
          &lt;/property&gt;
          &lt;property name="schemaUpdate"&gt;
              &lt;value&gt;true&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="hibernateProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.HSQLDialect
                  &lt;/prop&gt;
                  &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>