title: How to implement container managed transaction (CMT)?
body: <p>I wanted to persist an object(<code>ReportBean</code>) to the database, but I got error message:  </p>
  
  <pre><code>javax.persistence.TransactionRequiredException: Transaction is required to perform this operation (either use a transaction or extended persistence context)  
  </code></pre>
  
  <p>Here is a bit of a code:  </p>
  
  <p><strong>entity</strong></p>
  
  <pre><code>@Entity
  @Table(name="t_report")
  @Access(AccessType.FIELD)
  public class ReportBean implements Serializable {
  
      // fields (@Column, etc.)
      // setters/getters methods
      // toString , hashCode, equals methods
  }
  </code></pre>
  
  <p><strong>custom annotation</strong> for allowing EntityManager injection (with <code>@Inject</code>)</p>
  
  <pre><code>import javax.inject.Qualifier;
  import static java.lang.annotation.ElementType.*;
  import java.lang.annotation.Target;
  
  @Qualifier
  @Target({TYPE, METHOD, FIELD, PARAMETER})
  @Retention(RetentionPolicy.RUNTIME)
  public @interface MyEm {
  }
  </code></pre>
  
  <p><strong>EntityManager provider</strong></p>
  
  <pre><code>import javax.enterprise.inject.Produces;
  import javax.persistence.EntityManager;
  import javax.persistence.PersistenceContext;
  import javax.persistence.PersistenceContextType;
  
  public class EntityManagerProvider {
  
      private final String PERSISTENCE_UNIT = "MyPersistenceUnit";
  
      @SuppressWarnings("unused")
      @Produces
      @MyEm 
      @PersistenceContext(unitName=PERSISTENCE_UNIT, type=PersistenceContextType.TRANSACTION)
      private EntityManager em;
  
  }
  </code></pre>
  
  <p><strong>ValidateReportAction class</strong> - has a method to persist report to the database.<br>
  I am trying to stick with the most important imports.<br>
  If I want to use the <code>EntityManager</code> to create a query (or NamedQuery as in the example) everything works.</p>
  
  <pre><code>import javax.inject.Inject;
  import javax.inject.Named;
  import javax.persistence.EntityManager;
  import javax.ejb.TransactionAttribute;
  import javax.ejb.TransactionAttributeType;
  import javax.ejb.TransactionManagement;
  import javax.ejb.TransactionManagementType;
  import javax.enterprise.context.SessionScoped;
  
  
  @Named("validateReportAction")
  @SessionScoped
  @TransactionManagement(TransactionManagementType.CONTAINER)
  public class ValidateReportAction extends ReportAction implements Serializable {
  
      private static final long serialVersionUID = -2456544897212149335L;
  
      @Inject @MyEm
      private EntityManager em;
  
      @TransactionAttribute(TransactionAttributeType.REQUIRED)
      public synchronized String createReport() {
          ReportBean report = new Report();
          // set report properties
          // em.createNamedQuery("queryName").getResultList(); ---- works
          em.persist(report)
      }
  }
  </code></pre>
  
  <p><code>Q</code>: Here in the <code>createReport()</code> method when the em.persist executes is where the error appear. I thought that the transaction is managed by the container (<code>CMT</code>), but now I think I am wrong. Where have I made a mistake? What is the right way to implement CMT?  </p>
  
  <p>Here is also my <code>persistence.xml</code> configuration:  </p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence version="2.0"
      xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"&gt;
  
      &lt;persistence-unit name="MyPersistenceUnit" transaction-type="JTA"&gt;
  
          &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
          &lt;jta-data-source&gt;java:jboss/TimeReportDS&lt;/jta-data-source&gt;
          &lt;mapping-file&gt;META-INF/orm.xml&lt;/mapping-file&gt; 
  
          &lt;class&gt;....&lt;/class&gt;
          &lt;class&gt;....&lt;/class&gt;
          &lt;class&gt;....&lt;/class&gt;
  
          &lt;properties&gt;
  
              &lt;property name="jboss.entity.manager.factory.jndi.name"
                  value="java:/modelEntityManagerFactory" /&gt;
  
              &lt;!-- PostgreSQL Configuration File --&gt;
              &lt;property name="hibernate.connection.driver_class" value="org.postgresql.Driver" /&gt;
              &lt;property name="hibernate.connection.password" value="password" /&gt;
              &lt;property name="hibernate.connection.url" value="jdbc:postgresql://192.168.2.125:5432/t_report" /&gt;
              &lt;property name="hibernate.connection.username" value="username" /&gt;
  
              &lt;!-- Specifying DB Driver, providing hibernate cfg lookup
                   and providing transaction manager configuration --&gt;
              &lt;property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" /&gt;
              &lt;property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.JTATransactionFactory"/&gt;
              &lt;property name="hibernate.transaction.manager_lookup_class"
                  value="org.hibernate.transaction.JBossTransactionManagerLookup" /&gt;
              &lt;property name="hibernate.archive.autodetection" value="class" /&gt;
  
              &lt;!-- Useful configuration during development - developer can see structured SQL queries --&gt;
              &lt;property name="hibernate.show_sql" value="true" /&gt;
              &lt;property name="hibernate.format_sql" value="false" /&gt;
  
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt; 
  </code></pre>
  
  <p>Please let me know if something in my question is not clear.</p>