title: Proper way to autowire a Hibernate Session in a Spring Transaction JUnit test
body: <p>This question is similar to a previous <a href="https://stackoverflow.com/questions/734614/spring-hibernate-junit-no-hibernate-session-bound-to-thread">one</a>. I am trying to <code>@Autowire</code> a Hibernate Session in one of my Spring-JUnit-Transactional tests but I am getting this exception:</p>
  
  <p><code>java.lang.IllegalStateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional ...</code></p>
  
  <p>Here is my JUnit class:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations={"/applicationContext.xml"})
  @TransactionConfiguration(transactionManager="transactionManager")
  @Transactional
  public class MyTest {
      @Qualifier("session")
      @Autowired
      private Session session;
  
      @Test
      public void testSomething() {
          session.get(User.class, "me@here.com");
      }
  }
  </code></pre>
  
  <p>Every works fine if I <code>@Autowire</code> a <code>SessionFactory</code> and get my <code>Session</code> programmatically (instead of defining it in the Spring XML) like so:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations={"/applicationContext.xml"})
  @TransactionConfiguration(transactionManager="transactionManager")
  @Transactional
  public class MyTest{    
      @Qualifier("sessionFactory")
      @Autowired
      private SessionFactory sessionFactory;
  
      @Test
      public void testSomething() {
      Session session = SessionFactoryUtils.getSession(sessionFactory, false);
          session.get(User.class, "me@here.com");
      }
  }
  </code></pre>
  
  <p>I can, however, get my original example to work if I define my <code>Session</code> in my Spring XML with <code>&lt;aop:scoped-proxy /&gt;</code> like so:</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"
          xmlns:tx="http://www.springframework.org/schema/tx"
          xmlns:aop="http://www.springframework.org/schema/aop"
          xsi:schemaLocation="
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
          http://www.springframework.org/schema/tx
          http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
          http://www.springframework.org/schema/aop 
          http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
          "&gt;
  
      &lt;bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"&gt;
          ...
      &lt;/bean&gt;
  
      &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&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="configurationClass"&gt;
              &lt;value&gt;org.hibernate.cfg.AnnotationConfiguration&lt;/value&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
          &lt;property name="sessionFactory" ref="sessionFactory"/&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;/bean&gt;
  
      &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt;
  
      &lt;bean id="session" class="org.springframework.orm.hibernate3.SessionFactoryUtils" factory-method="getSession" scope="prototype"&gt;
          &lt;constructor-arg ref="sessionFactory" /&gt;
          &lt;constructor-arg value="false" /&gt;
          &lt;!-- This is seems to be needed to get rid of the 'No Hibernate Session' error' --&gt;
          &lt;aop:scoped-proxy /&gt;
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>My question is: Why is <code>&lt;aop:scoped-proxy /&gt;</code> needed given that there should only one thread-bounded transaction context in my unit test? What <strong>is</strong> the proper way to define my <code>Hibernate Session</code> bean?</p>