title: How to start a transaction in JPA using entityManager
body: <p>I have started working on an application which uses spring, hibernate, JPA, SOAP webservices. Now there is a requirement that certain queries have to be run in a transaction. If any one fails, entire transaction should rollback. </p>
  
  <p>The code in the dao layer is as follows :</p>
  
  <pre><code>import javax.persistence.EntityManager;
  import javax.persistence.EntityTransaction;
  import javax.persistence.PersistenceContext;
  import javax.persistence.PersistenceContextType;
  import javax.persistence.Query;
  import org.hibernate.Session;
  
      public class BillDAOImpl implements BillDao{
  
               @PersistenceContext(type = PersistenceContextType.EXTENDED)
               private EntityManager em;
  
               public boolean processBills() throws Exception{
  
               EntityTransaction tx = null;
               Session session = null;
  
               try{
  
                   session = em.unwrap(Session.class);
                   tx = em.getTransaction();
  
                   Bill bill = em.find(Bill.class, billId);
  
                   //session.beginTransaction();
                   tx.begin();
                   ...
                   ...
                   em.persist(bill);
                   ...
                   ...
                   em.merge(&lt;other object&gt;);
                   ...
                   ...
                   //session.getTransaction().commit();
                   tx.commit();
               } catch(){
               }
  
               }
  
      }
  </code></pre>
  
  <p>When it executes <code>tx = em.getTransaction()</code>, it gives following error :</p>
  
  <pre><code>java.lang.IllegalStateException: Cannot execute getTransaction() on a container-managed EntityManager
  </code></pre>
  
  <p>The other transaction related properties are as follows :</p>
  
  <pre><code>&lt;bean id="tuneEntityManagerFactory"
          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
          p:persistenceXmlLocation="classpath*:META-INF/tune-persistence.xml"
          p:persistenceUnitName="tunePersistenceUnit" p:loadTimeWeaver-ref="loadTimeWeaver"
          p:jpaVendorAdapter-ref="jpaVendorAdapter" p:jpaDialect-ref="jpaDialect"
          p:dataSource-ref="tuneDbDataSource"&gt;
          &lt;property name="jpaProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.transaction.manager_lookup_class"&gt;org.hibernate.transaction.BTMTransactionManagerLookup
                  &lt;/prop&gt;
                  &lt;prop key="net.sf.ehcache.configurationResourceName"&gt;/${tune-db.ehcache.config.file}&lt;/prop&gt;
                  &lt;prop key="hibernate.transaction.flush_before_completion"&gt;false&lt;/prop&gt;              
                  &lt;prop key="hibernate.default_schema"&gt;${tune-db.schema}&lt;/prop&gt;
                  &lt;prop key="org.hibernate.envers.default_schema"&gt;${tune-db.schema}&lt;/prop&gt;
                  &lt;prop key="javax.persistence.validation.mode"&gt;${tune-db.data.validation}&lt;/prop&gt;
                  &lt;prop key="hibernate.connection.isolation"&gt;3&lt;/prop&gt;
                  &lt;prop key="hibernate.connection.release_mode"&gt;auto&lt;/prop&gt;
                  &lt;prop key="hibernate.show_sql"&gt;${tune-db.hibernate.show-sql}&lt;/prop&gt;
                  &lt;prop key="hibernate.format_sql"&gt;${tune-db.hibernate.format-sql}&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;     
      &lt;/bean&gt;
  
  &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
          &lt;property name="entityManagerFactory" ref="tuneEntityManagerFactory" /&gt;
      &lt;/bean&gt;
  </code></pre>
  
  <p>When I use <code>session.beginTransaction()</code> and <code>session.getTransaction().commit()</code>, it works correctly.</p>
  
  <p>However I want to replace it with transaction from <code>entityManager</code>. Then what should be done?</p>