title: Configuring Spring + Hibernate JPA Transaction Manager through JTA
body: <p>I previously had this config for Hibernate using RESOURCE-LOCAL transaction type:</p>
  
  <p>persistence.xml:</p>
  
  <pre><code>&lt;persistence-unit name="myPU" transaction-type="JTA"&gt;
          &lt;provider&gt;org.hibernate.jpa.HibernatePersistenceProvider&lt;/provider&gt;
  
  &lt;/persistence-unit&gt;
  </code></pre>
  
  <p>applicationContext (dataaccess bit):</p>
  
  <p></p>
  
  <pre><code>&lt;bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /&gt;
  
  &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" p:entityManagerFactory-ref="entityManagerFactory"&gt;&lt;/bean&gt;
  
  &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;property name="jpaVendorAdapter" ref="jpaAdapter" /&gt;
      &lt;property name="persistenceUnitName" value="myPU"/&gt;
      &lt;property name="jpaProperties"&gt;
          &lt;props&gt;
              &lt;prop key="javax.persistence.validation.mode"&gt;none&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="entityManager" class="org.springframework.orm.jpa.support.SharedEntityManagerBean"&gt;
      &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="sessionFactory" class="org.springframework.orm.jpa.vendor.HibernateJpaSessionFactoryBean"&gt;
      &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt;
      &lt;!-- Are there any other properties required? --&gt;
  &lt;/bean&gt;
  
  &lt;bean id="jpaAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
  
      &lt;property name="showSql" value="true" /&gt;
      &lt;property name="generateDdl" value="false" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"&gt;
      &lt;property name="jndiName" value="java:comp/env/jdbc/CNTXESDB" /&gt;
      &lt;property name="lookupOnStartup" value="true" /&gt;
      &lt;property name="cache" value="true" /&gt;
      &lt;property name="proxyInterface" value="javax.sql.DataSource" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>But this kind of transaction seems not to work with Glassfish, so I had to switch to JTA transactions.</p>
  
  <p>The problem is -- to get Spring to manage transaction creation (through @Transactional) I need to define a <code>TransactionManager</code> bean but <code>JtaTransactionManager</code> included in spring-tx does not accept an entityManagerFactory bean, so it does not know where the entityManager is in order to open/close/flush Hibernate session.</p>
  
  <p>So how can I configure Spring with Hibernate to use JTA transactions?</p>
  
  <p><strong>EDIT:</strong>
  turns out you <strong>can</strong> use RESOURCE_LOCAL transactions with Glassfish, but somehow you cannot have a persistence.xml file. I renamed this file to <code>my_persistence.xml</code> and configured <code>LocalContainerEntityManagerFactoryBean</code> like this:</p>
  
  <pre><code>&lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
          &lt;property name="jpaVendorAdapter" ref="jpaAdapter" /&gt;
          &lt;property name="persistenceUnitName" value="myPU"/&gt;
          &lt;property name="persistenceXmlLocation" value="classpath:META-INF/my_persistence.xml" /&gt;
          &lt;property name="jpaProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="javax.persistence.validation.mode"&gt;none&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  </code></pre>