title: A JTA EntityManager cannot use getTransaction()
body: <p>How can I use the following code in my non-ejb application. The code works.</p>
  <pre><code>@Override
  public void saveItems(Collection&lt;T&gt; items) {
      synchronized (em) {
          EntityTransaction tx = em.getTransaction();
          try {
              tx.begin();
              for (T item : items) {
                  saveItem_((Class&lt;T&gt;) null, item);
              }
              tx.commit();
          } finally {
              if (tx.isActive()) {
                  tx.rollback();
              }
          }
      }
  }
  </code></pre>
  <p>In a new application I'm using EJB3 + JSF and would like to re-use the library containing the code above. My peristence unit for the new application looks like this:</p>
  <pre><code>  &lt;persistence-unit name=&quot;myApp&quot; transaction-type=&quot;JTA&quot;&gt;
      &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
      &lt;jta-data-source&gt;MySQLConnection&lt;/jta-data-source&gt;
    &lt;/persistence-unit&gt;
  </code></pre>
  <p>My new application throw an exception when it hits this line:</p>
  <pre><code>    EntityTransaction tx = em.getTransaction();
  </code></pre>
  <p>the exception is:</p>
  <pre><code>A JTA EntityManager cannot use getTransaction()
  </code></pre>
  <p>Which is clear enough. The question is how would I convert my code to have the transactions managed by the container. Presumably my bean methods need to be annotated appropriately... The question is how?</p>