title: JTA EntityManager cannot use getTransaction() [Spring + Hibernate + EntityManager]
body: <p>I am using Spring + JPA + Hibernate + EntityManager to talk to the database. I am getting <em>'A JTA EntityManager cannot use getTransaction()</em>' error. Please provide your insights and help me resolve the issue. </p>
  
  <p><strong>beans.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans default-autowire="byName"
      ... xmlns definitions...
        xsi:schemaLocation="..."&gt;
  
      &lt;context:component-scan base-package="com.mycompany.myproject" /&gt;
  
      &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" /&gt;
      &lt;bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /&gt;
  
      &lt;bean id="myDAO" class="com.mycompany.myproject.dao.myDAO" /&gt;
  
      &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
          &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt;
      &lt;/bean&gt;
  
      &lt;tx:annotation-driven /&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>persistence.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence ... xmlns definitions xsi:schemaLocation="..." version="1.0"&gt;
      &lt;persistence-unit name="TEST_DS"&gt;
          &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
          &lt;jta-data-source&gt;java:/TEST_DS&lt;/jta-data-source&gt;
          &lt;class&gt;com.twinspires.exchange.model.Test&lt;/class&gt;
          &lt;properties&gt;
              &lt;property name="hibernate.archive.autodetection" value="class, hbm" /&gt;
              &lt;property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" /&gt;
              &lt;property name="hibernate.hbm2ddl.auto" value="validate" /&gt; &lt;!-- create-drop update --&gt;
              &lt;property name="hibernate.cache.use_query_cache" value="true" /&gt;
              &lt;property name="hibernate.show_sql" value="true" /&gt;
              &lt;property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider" /&gt;
                &lt;property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/&gt;             
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p><strong>Exception Stack Trace (Excerpt)</strong></p>
  
  <pre><code>15:47:43,340 INFO  [STDOUT] DEBUG: org.springframework.transaction.annotation.AnnotationTransactionAttributeSource - Adding transactional method 'getName' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
  15:47:43,343 INFO  [STDOUT] DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'transactionManager'
  15:47:43,356 INFO  [STDOUT] DEBUG: org.springframework.orm.jpa.JpaTransactionManager - Creating new transaction with name [com.twinspires.exchange.dao.PicDAO.getName]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
  15:47:44,114 INFO  [STDOUT] DEBUG: org.springframework.orm.jpa.JpaTransactionManager - Opened new EntityManager [org.hibernate.ejb.EntityManagerImpl@c629e5] for JPA transaction
  15:47:44,124 INFO  [STDOUT] DEBUG: org.springframework.orm.jpa.JpaTransactionManager - Could not rollback EntityManager after failed transaction begin
  15:47:44,125 INFO  [STDOUT] java.lang.IllegalStateException: A JTA EntityManager cannot use getTransaction()
  15:47:44,125 INFO  [STDOUT]     at org.hibernate.ejb.AbstractEntityManagerImpl.getTransaction(AbstractEntityManagerImpl.java:818)
  15:47:44,126 INFO  [STDOUT]     at org.springframework.orm.jpa.JpaTransactionManager.closeEntityManagerAfterFailedBegin(JpaTransactionManager.java:412)
  15:47:44,127 INFO  [STDOUT]     at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:381)
  15:47:44,128 INFO  [STDOUT]     at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:371)
  15:47:44,129 INFO  [STDOUT]     at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:335)
  15:47:44,129 INFO  [STDOUT]     at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:105)
  15:47:44,130 INFO  [STDOUT]     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
  15:47:44,131 INFO  [STDOUT]     at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
  15:47:44,131 INFO  [STDOUT]     at $Proxy175.getName(Unknown Source)
  </code></pre>
  
  <p>Folowing is my dao class:-</p>
  
  <pre><code>public class MyDao implements IMyDao {
        @PersistenceContext(unitName = "TEST_DS")
        private EntityManager entityManager;
  
        @Transactional
        public String getName() {            
              final Query query = entityManager.createQuery("from TestTable");
              final Object obj = query.getResultList().get(0);
              return obj == null ? "Empty" : (String) obj;
        }
  }
  </code></pre>
  
  <p>Your help highly appreciated.</p>