title: Object is not getting persisted using JPA/JTA/JBOSS/CDI
body: <p>Please help me to understand why object is not getting persisted with following code. It throws <strong><em>javax.persistence.TransactionRequiredException: JBAS011469: Transaction is required to perform this operation (either use a transaction or extended persistence context)</em></strong></p>
  
  <pre><code>public class OrganizationRepositoryImpl implements OrganizationRepository {
  
      @PersistenceContext(unitName="usermanagement",type=PersistenceContextType.TRANSACTION)
      private EntityManager em;
  
      public void save(Organization organization) {
          try {
              em.persist(organization);
          }catch(Exception e) {
              e.printStackTrace();
  
          }
      }    
  }
  </code></pre>
  
  <p>But If I annotate the class with <strong>@Stateless</strong> annotation(Now ejb), object start getting persisted</p>
  
  <pre><code>@Stateless    
  public class OrganizationRepositoryImpl implements OrganizationRepository {
  
              @PersistenceContext(unitName="usermanagement",type=PersistenceContextType.TRANSACTION)
              private EntityManager em;
  
              public void save(Organization organization) {
                  try {
                      em.persist(organization);
                  }catch(Exception e) {
                      e.printStackTrace();
  
                  }
              }    
          }
  </code></pre>
  
  <p><strong>persistence.xml</strong></p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
               http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
          version="2.1"&gt;
      &lt;persistence-unit name="usermanagement" transaction-type="JTA"&gt;
          &lt;jta-data-source&gt;java:jboss/datasources/MysqlXADS&lt;/jta-data-source&gt;
          &lt;exclude-unlisted-classes&gt;false&lt;/exclude-unlisted-classes&gt;
          &lt;properties&gt;
               &lt;property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/&gt;
               &lt;property name="hibernate.hbm2ddl.auto" value="create-drop"/&gt;
               &lt;property name="hibernate.show_sql" value="true"/&gt;
               &lt;property name="hibernate.format_sql" value="true"/&gt;  
               &lt;property name="use_sql_comments" value="true"/&gt;
               &lt;property name="jboss.entity.manager.factory.jndi.name" value="java:jboss/myEntityManagerFactory" /&gt; 
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p><strong>Service class to call Repository to persist the object to the database</strong></p>
  
  <pre><code>    public class OrganizationServiceImpl implements OrganizationService {
  
      @Inject
      private OrganizationRepository orgRepo;
  
      public Response createOrganization(InputStream is) {
          Organization org = null;
          org = readStream(is);
          orgRepo.save(org);
          return Response.created(URI.create("/organizations/" + org.getId()))
                  .build();
      }
  
      private Organization readStream(InputStream is) {
          JAXBContext context;
          Organization org = null;
          try {
              context = JAXBContext.newInstance(Organization.class);
              Unmarshaller um = context.createUnmarshaller();
              org = (Organization) um.unmarshal(is);
          } catch (JAXBException e) {
              e.printStackTrace();
          }
          return org;
      }
  }  
  </code></pre>