title: Why need to clear jpa persistence context before executing modifying query?
body: <p>I'm working with Spring Boot 1.5.2.RELEASE and a PostgreSQL 9.5 database.</p>
  
  <p>File entity:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Entity
  public class File implements Serializable {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
  
      private String externalUid;
  }
  </code></pre>
  
  <p>File repository looks like:</p>
  
  <pre class="lang-java prettyprint-override"><code>public interface FileRepository extends JpaRepository&lt;File, Long&gt; {
  
      @Modifying
      @Query("UPDATE File f SET f.externalUid =:externalUid WHERE f.id =:id")
      void setExternalLink(@Param("id") Long id, @Param("externalUid") String externalId);
  }
  </code></pre>
  
  <p>After when I save new file (with flush) and execute update on it, then get this file from repository, then I have outdated entity. But when I have executed <code>clear()</code> [1] on <code>entityManager</code> before executing modifying query, then I get the updated entity:</p>
  
  <pre><code>String externalUid = UUID.randomUUID().toString();
  
  File file = new File();
  File savedFile = fileRepository.saveAndFlush(file);
  
  // [1] entityManager.clear();
  
  fileRepository.setExternalLink(savedFile.getId(), externalUid);
  
  File updatedFile = fileRepository.findOne(savedFile.getId());
  
  System.out.print(updatedFile.getExternalUid()); // null
  </code></pre>
  
  <p>This issue quite similar with this one: <a href="https://stackoverflow.com/questions/32258857/spring-boot-data-jpa-modifying-update-query-refresh-persistence-context">Spring Boot Data JPA - Modifying update query - Refresh persistence context</a>. But in my case I execute <code>saveAndFlush()</code> instead of <code>save()</code>.</p>
  
  <p>In that regard can somebody explain me why we need to clear persistence context before executing modifying query and why repository returns me the outdated entity?</p>