title: EntityManager.setFlushMode() vs Query.setFlushMode()
body: <p><strong>Some documentation references:</strong></p>
  
  <p>Enum javax.persistence.FlushModeType</p>
  
  <ul>
  <li>AUTO:   (Default) Flushing to occur at query execution.</li>
  <li>COMMIT: Flushing to occur at transaction commit.</li>
  </ul>
  
  <p>javax.persistence.EntityManager.setFlushMode(FlushModeType flushMode)</p>
  
  <ul>
  <li>Set the flush mode that applies to all objects contained in the persistence context. </li>
  </ul>
  
  <p>javax.persistence.Query.setFlushMode(FlushModeType flushMode)</p>
  
  <ul>
  <li>Set the flush mode type to be used for the query execution. <strong><em>The flush mode type applies to the query regardless of the flush mode type in use for the entity manager.</em></strong> </li>
  </ul>
  
  <p><strong>Sample Code:</strong></p>
  
  <pre><code>PTEmp e = em.find(PTEmp.class, 7852L);
  e.setName("AAA");
  
  String updateSqlString = "UPDATE PTEmp SET name = :name WHERE id = :id";
  TypedQuery&lt;PTEmp&gt; query = em.createQuery(updateSqlString, PTEmp.class);
  query.setParameter("name", "BBB");
  query.setParameter("id", 7852L);
  
  query.setFlushMode(FlushModeType.AUTO); // &lt;= in 2nd test-case 
                                          //    this line is commented
  em.setFlushMode(FlushModeType.COMMIT);
  
  System.out.println("query FMT: " + query.getFlushMode()); // Always print: AUTO
  System.out.println("em    FMT: " + em.getFlushMode());    // Always print: COMMIT
  
  em.getTransaction().begin();
  query.executeUpdate();
  em.getTransaction().commit();
  </code></pre>
  
  <p><strong>Test-cases: different combinations of FlushModeTypes for entityManager and query. Also shows the SQL logs:</strong></p>
  
  <ol>
  <li><p>Test-case: explicitly query with AUTO FlushModeType (query.setFlushMode(FlushModeType.AUTO); uncommented):</p>
  
  <pre><code>1594 Query     UPDATE PTEMP SET fullname = 'AAA' WHERE (ID = 7852)
  1594 Query     UPDATE PTEMP SET fullname = 'BBB' WHERE (ID = 7852)
  1594 Query     COMMIT
  </code></pre></li>
  <li><p>Test-case: implicit (Default value) query with AUTO FlushModeType (query.setFlushMode(FlushModeType.AUTO); commented):</p>
  
  <pre><code>1616 Query     UPDATE PTEMP SET fullname = 'BBB' WHERE (ID = 7852)
  1616 Query     UPDATE PTEMP SET fullname = 'AAA' WHERE (ID = 7852)
  1616 Query     COMMIT
  </code></pre></li>
  </ol>
  
  <p>In both test cases the System.out.println() returns:</p>
  
  <ul>
  <li>query flushModeType: AUTO     </li>
  <li>em    flushModeType: COMMIT </li>
  </ul>
  
  <p><strong>My doubt:</strong></p>
  
  <p>I don't understand why these two test cases execute the queries in different order; when the flushModeTypes in both cases are the same.
  The consequence is that finally the 1st-test-case persists 'BBB' and the 2nd-test-case 'AAA'.</p>
  
  <p>In my opinion both test cases ought to execute the sql query in the same order and the right order ought to be like in 1st-test-case (before: 'AAA', after: 'BBB'), because the query has flushModeType=AUTO the persistence context should be flushed to the db before execute the query.</p>
  
  <p>My persistence provider: EclipseLink.</p>
  
  <p>DB: mariadb.</p>