title: Getting javax.persistence.TransactionRequiredException on JPA + JAX-RS
body: <p>I'm having a troubles with my JavaEE application. I'm running Hibernate + Jersey on TomEE.</p>
  
  <p>This is my base class:</p>
  
  <pre><code>public abstract class BaseEntityManager&lt;E, K&gt;{
  protected Class entityClass;
  
  @PersistenceContext
  protected EntityManager em;
  
  public BaseEntityManager() {
      ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
      this.entityClass = (Class) genericSuperclass.getActualTypeArguments()[0];
  }
  
  public EntityManager getManager() {
      return em;
  }
  
  public void persist(E entity) { em.persist(entity); }
  
  public void remove(E entity) { em.remove(entity); }
  
  public E findById(K primaryKey) { return (E)em.find(entityClass, primaryKey); }
  
  
  }
  </code></pre>
  
  <p>And this is the class for concrete type:</p>
  
  <pre><code>@Stateless
  public class SomethingManager extends BaseEntityManager&lt;SomethingEntity, Integer&gt;{}
  </code></pre>
  
  <p>And here is my resource. I deserialize data from JSON in my JSONCreator. There are some IDs of referenced entities. Then in the method "new" I try to persist new object which works fine, however I cannot merge the relation from the other entities back to the new object (the IDs of referenced entities I mentioned earlier).</p>
  
  <pre><code>@Path("/Something")
  public class Something{
  
  // Other entity managers here ....
  @EJB
  SomethingManager smm;
  
  @EJB
  OtherManager ttem;
  
  
  @Secured
  @POST
  @Path("/new")
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public SomethingEntity add(@Context HttpServletRequest req, SomethingEntity r)
  {
     ...
  
  
      try {
  
  
          for (int i = 0; i &lt; r.getOtherEntities().size(); i++) {
              OtherEntity x = (r.getOtherEntities().get(i));
              x = ttem.findById(x.getId());
              x.getSomethingEntities().add(r);
              ttem.getManager().merge(x);
          }
      }
      catch (Exception e)
      {
          e.printStackTrace(); // TODO Intelligent logging
          return null;
      }
  
      return r;
      }
  }
  </code></pre>
  
  <p>This throws the exception ttem.getManager().merge(x);</p>
  
  <p>What seems to be an issue?</p>
  
  <p>Thank you.</p>