title: Force refresh of collection JPA entityManager
body: <p>I am using SEAM with JPA (implemented as a Seam Managed Persistance Context), in my backing bean I load a collection of entities (ArrayList) into the backing bean.</p>
  
  <p>If a different user modifies one of the entities in a different session I want these changes to be propagated to the collection in my session, I have a method <code>refreshList()</code> and have tried the following...</p>
  
  <pre><code>@Override
  public List&lt;ItemStatus&gt; refreshList(){
      itemList = itemStatusDAO.getCurrentStatus();
  }
  </code></pre>
  
  <p>With the following query</p>
  
  <pre><code>@SuppressWarnings("unchecked")
  @Override
  public List&lt;ItemStatus&gt; getCurrentStatus(){
      String s = "SELECT DISTINCT iS FROM ItemStatus iS ";
      s+="ORDER BY iS.dateCreated ASC";
      Query q = this.getEntityManager().createQuery(s);
      return q.getResultList();
  }
  </code></pre>
  
  <p>Re-executing the query, this just  returns the same data I already have (I assume it is using the 1st level cache rather than hitting the database)</p>
  
  <pre><code>@Override
  public List&lt;ItemStatus&gt; refreshList(){
      itemStatusDAO.refresh(itemList)
  }
  </code></pre>
  
  <p>Calling <code>entityManager.refresh()</code>, this should refresh from the database however I get a <code>javax.ejb.EJBTransactionRolledbackException: Entity not managed</code> exception when I use this, normally I would use <code>entityManager.findById(entity.getId)</code> before calling .refresh() to ensure it is attached to the PC but as I am refreshing a collection of entities I cant do that.</p>
  
  <p>It seems like quite a simple problem, I cant believe there is no way to force JPA/hibernate to bypass the cache and hit the database?!</p>
  
  <p>UPDATE TEST CASE:</p>
  
  <p>I am using two different browsers (1 and 2) to load the same web page, I make a modification in 1 which updates a boolean attribute in one of the ItemStatus entities, the view is refreshed for 1 to display the updated attribute, I check the database via PGAdmin and the row has been updated. I then press refresh in Browser 2 and the attribute has not been updated</p>
  
  <p>I tried using the following method to merge all entities before calling .refresh, but the entities were still not updated from the database.</p>
  
  <pre><code>@Override
  public void mergeCollectionIntoEntityManager(List&lt;T&gt; entityCollection){
      for(T entity: entityCollection){
          if(!this.getEntityManager().contains(entity)){
              this.getEntityManager().refresh(this.getEntityManager().merge(entity));
          }
      }
  }
  </code></pre>