title: @Stateless webservice with JPA+JTA: How to commit changes of managed entity?
body: <p>I have the following simple webservice declared as <code>@Stateless</code> EJB running on GlassFish 3.1.2.2 with EclipseLink 2.4.1 using a JTA <code>DataSource</code> to connect to a MySQL database:</p>
  
  <pre><code>@POST
  @Consumes(MediaType.APPLICATION_JSON)
  public Response update(TimeRow e) throws Exception {
      if ((e.getKey() == null) || !e.getKey().keyValid()) {
          return Response.status(400).build();
      }
  
      TimeRow existing = em.find(TimeRow.class, e.getKey());
      if (existing == null) {
          em.persist(e);
      } else {
          existing.setValues(e.getValues());
          em.flush();
      }
      return Response.status(204).build();
  }
  </code></pre>
  
  <p>Entity class of <code>TimeRow</code>:</p>
  
  <pre><code>@Entity
  @NamedQueries({
      @NamedQuery(name="TimeRow.findAllByUser",
           query="SELECT t FROM TimeRow t WHERE t.table.userId = :uid")
  })
  public class TimeRow implements Serializable {
  
      @EmbeddedId
      private TimeRowPK key;
  
      @MapsId("userId")
      @JoinColumn(name = "USERID", referencedColumnName = "userId")
      @ManyToOne
      private UserTable table;
      @Column(name="ROWVALUES")
      private List&lt;Double&gt; values;
  
      public TimeRow() {
          this.key = new TimeRowPK();
          this.values = new ArrayList&lt;Double&gt;(20);
          extendValuesTo20();
      }
  
      public TimeRow(String uid, Date date) {
          this.key = new TimeRowPK(date, uid);
          this.table = new UserTable(uid);
          this.values = new ArrayList&lt;Double&gt;(20);
          extendValuesTo20();
      }
  
      public List&lt;Double&gt; getValues() {
          return values;
      }
  
      public void setValues(List&lt;Double&gt; values) {
          this.values = values;
          extendValuesTo20();
      }
  
      private void extendValuesTo20() {
          if (this.values.size() &lt; 20) {
               for (int i = this.values.size(); i &lt; 20; i++) {
                    this.values.add(0.0);
               }
          }
      }
  
  }
  </code></pre>
  
  <p><code>@EmbeddableId</code> <code>TimeRowPK</code>:</p>
  
  <pre><code>@Embeddable
  public class TimeRowPK implements Serializable {
  
      public TimeRowPK() { }
  
      public TimeRowPK(Date date, String userId) {
          this.date = date;
          this.userId = userId;
      }
  
      @Column(name="DAY")
      private Date date;
      @Column(name = "USERID")
      private String userId;
  
      public boolean keyValid() {
          return ((date != null) &amp;&amp; ((userId != null) &amp;&amp; !userId.isEmpty()));
      }
  
  }
  </code></pre>
  
  <p><code>persistence.xml</code> (without the <code>&lt;persistence&gt;</code> tag):</p>
  
  <pre><code>&lt;persistence-unit name="test" transaction-type="JTA"&gt;
      &lt;provider&gt;org.eclipse.persistence.jpa.PersistenceProvider&lt;/provider&gt;
      &lt;jta-data-source&gt;jdbc/test&lt;/jta-data-source&gt;
      &lt;class&gt;com.test.TimeRow&lt;/class&gt;
      &lt;class&gt;com.test.TimeRowPK&lt;/class&gt;
      &lt;class&gt;...&lt;/class&gt;
      &lt;properties&gt;
          &lt;property name="eclipselink.ddl-generation" value="create-tables" /&gt;
          &lt;property name="eclipselink.ddl-generation.output-mode" value="database" /&gt;
      &lt;/properties&gt;
  &lt;/persistence-unit&gt;
  </code></pre>
  
  <p>Webservice declaration:</p>
  
  <pre><code>@Path("row")
  @Stateless
  public class TimeRowWebService {
  
      @PersistenceContext(unitName = "test")
      private EntityManager em;
  
      ...
  
  }
  </code></pre>
  
  <p>The problem is that if the entity exists, the changes get stored only in the <code>PersistenceContext</code>, but they are not committed to the database. Meaning that I can retrieve the correct data with the changes, but for example if I restart the AS, the changes are gone. There are no errors in the AS log.</p>
  
  <p>So I guess I have to do some bean level manual transaction handling to get this work. What exactly do I have to add to get this working?</p>