title: JPA entity updated only after method is done
body: <p>I have an Asynchronous annotated method which receives an entity though the method parameter. In this method I try to set a variable three times:</p>
  
  <pre><code>@Inject EntityDao entityDao;
  
  @Asynchronous
  public Future&lt;String&gt; doSomething (MyEntity p_myEntity) {
  
      MyEntity myEntity = entityDao.merge(p_myEntity); // change from detached to attached
      // em.contains(myEntity) returns true
  
      myEntity.setName("Joe 1"); // newer set in database
      // A System.out.println(myEntity.getName()) does say "Joe 1"
  
      try {
          Thread.sleep(20*1000);
      } catch () ...etc
  
      myEntity.setName("Joe 2"); // newer set in database
      // A System.out.println(myEntity.getName()) does say "Joe 2"
  
      try {
          Thread.sleep(20*1000);
      } catch () ...etc
  
      myEntity.setName("Joe 3"); // only one set in database
  
      return new AsyncResult&lt;&gt;("done");
  }
  </code></pre>
  
  <p><strong>Edit</strong>: So thanks to PedroKowalski I have a better understanding of the problem and I will reformulate.</p>
  
  <p>During the execution of the above method, I have two ways of checking if the myEntity.setName() is actually changed:</p>
  
  <ul>
  <li>During the sleep() I check the database if the name value is changed</li>
  <li>On a webpage the list of MyEntity objects are displayed (with the names), this list is being updated every 2 seconds.</li>
  </ul>
  
  <p>Both of the above methods indicate that the values "Joe 1" and "Joe 2" are newer in the database. Only after the doSomething() method is done, the last name set (Joe 3) is being put in the database.</p>
  
  <p>So my question is: why are the values "Joe 1" and "Joe 2" not put in the database and only the last value is put in the database?</p>