title: Using Spring defined transactionManager in JPA/Hibernate
body: <p>Suppose you use JPA with Spring, with Hibernate as JPA implementation.
  JPA transaction mode is "JTA", so you need to pass the container transactionManager to Hibernate.
  The classical answer is to set <code>hibernate.transaction.manager_lookup_class</code> to the matching class for your server.</p>
  
  <p>However, I think it's a shame to have this depend of server specific configuration as you already found the transactionManager in Spring with <code>&lt;tx:jta-transaction-manager&gt;</code>.</p>
  
  <p>Is there a way to give this transactionManager to Hibernate with a configuration like</p>
  
  <pre><code> &lt;bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
    &lt;property name="persistenceUnitName" value="persistence_unit_name"/&gt;
    &lt;property name="jpaVendorAdapter"&gt;
     &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/&gt;
    &lt;/property&gt;
    &lt;property name="jpaProperties"&gt;
     &lt;props&gt;
      &lt;prop key="hibernate.transaction.manager_lookup_class"&gt;
       org.hibernate.transaction.SunONETransactionManagerLookup
      &lt;/prop&gt;
     &lt;/props&gt;
    &lt;/property&gt;
   &lt;/bean&gt;
  
   &lt;tx:jta-transaction-manager/&gt;
  </code></pre>
  
  <p>The goal is to get rid of the <code>org.hibernate.transaction.SunONETransactionManagerLookup</code> property.
  By the way, I really have two different server implementations in mind.</p>
  
  <p>EDIT : without the transaction manager configuration, Hibernate chokes when creating the EntityManagerFactory :</p>
  
  <pre><code>Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in URL [file:/C:/configuration/afoCuad-metier-ear/entitymanager-base-context.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: fr.tm.ima.cuad-afoCuad-metier-ejb-PU] Unable to build EntityManagerFactory
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1420)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
  at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
  at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
  at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
  at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
  at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findDefaultEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:529)
  at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:495)
  at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.resolveEntityManager(PersistenceAnnotationBeanPostProcessor.java:656)
  at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.getResourceToInject(PersistenceAnnotationBeanPostProcessor.java:629)
  at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:147)
  at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
  at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:338)
  ... 80 more
  Caused by: javax.persistence.PersistenceException: [PersistenceUnit: fr.tm.ima.cuad-afoCuad-metier-ejb-PU] Unable to build EntityManagerFactory
  at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:901)
  at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:74)
  at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:225)
  at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:308)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
  ... 93 more
  Caused by: org.hibernate.HibernateException: The chosen transaction strategy requires access to the JTA TransactionManager
  at org.hibernate.impl.SessionFactoryImpl.&lt;init&gt;(SessionFactoryImpl.java:401)
  at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1385)
  at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:954)
  at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:892)
  ... 98 more
  </code></pre>