title: How to inject SessionFactory when persistence unit is defined?
body: <p>I have a fully working Spring 4 + Hibernate application. Hibernate is configured via <code>&lt;persistence-unit&gt;</code>... My unit test does all DB-related work without a problem. But now I want to access Hibernate session; to do that I should inject <code>SessionFactory</code>. How to inject it considering the following configuration?</p>
  
  <p>I know I could define a bean like <code>LocalSessionFactoryBean</code> but don't know how to configure it. (I want no duplicated aconfiguration also.)</p>
  
  <p>MyTest.java</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = { "classpath:META-INF/spring/test-context.xml" })
  @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class, TransactionalTestExecutionListener.class})
  public class MyTest {
  
      @Inject
      SessionFactory sessionFactory; // want it injected
  
  }
  </code></pre>
  
  <p>test-context.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans&gt;
      &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
          &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt;
      &lt;/bean&gt;
      &lt;context:annotation-config/&gt;
      &lt;context:component-scan base-package="com.mycompany"/&gt;
      &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt;
      &lt;bean id="hibernateJpaVendorAdapter"
            class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/&gt;
      &lt;bean id="entityManagerFactory"
            class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"&gt;
          &lt;property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/&gt;
          &lt;property name="persistenceUnitName" value="my-persistence-unit"/&gt;
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>persistence.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence xmlns="..." version="2.0"&gt;
      &lt;persistence-unit name="my-persistence-unit" transaction-type="JTA"&gt;
          &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
          &lt;jta-data-source&gt;java:jboss/datasources/my&lt;/jta-data-source&gt;
          &lt;class&gt;com.mycompany.entity.Document&lt;/class&gt;
          &lt;exclude-unlisted-classes&gt;false&lt;/exclude-unlisted-classes&gt;
          &lt;properties&gt;
              &lt;property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/&gt;
              &lt;property name="hibernate.hbm2ddl.auto" value="validate"/&gt;
              &lt;property name="hibernate.show_sql" value="true"/&gt;
              &lt;property name="hibernate.format_sql" value="false"/&gt;
              &lt;property name="hibernate.archive.autodetection" value="class, hbm"/&gt;
              &lt;property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform"/&gt;
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>