title: How to use EntityManager to find an entity by id without hitting cache in one query?
body: <p>I have a critical section of code where I need to read and lock an entity by id with pessimistic lock.</p>
  
  <p>This section of code looks like this right now:</p>
  
  <pre><code>MyEntity entity = entityManager.find(MyEntity.class, key);
  entityManager.refresh(entity, LockModeType.PESSIMISTIC_WRITE);
  </code></pre>
  
  <p>It works OK, but as I understand in case when there is no entity in the hibernate's cache, we will use 2 read transactions to a database. 1st transaction to find the entity by id and another transaction to refresh and lock the entity. 
  Is it possible to use only one transaction in such scenario?</p>
  
  <p>I would imagine something like:</p>
  
  <pre><code>boolean skipCache = true;
  MyEntity entity = entityManager.find(MyEntity.class, key, 
      LockModeType.PESSIMISTIC_WRITE, skipCache);
  </code></pre>
  
  <p>But there is no such parameter like <code>skipCache</code>. Is there another approach to read an entity by id directly from the database by using <code>EntityManager</code>?</p>
  
  <p>UPDATE:</p>
  
  <p>This query will hit the first level cache in case the entity exists in the cache. Thus, it may potentially return the outdated data and that is why isn't suitable for critical sections where any read should be blocked:</p>
  
  <pre><code>MyEntity entity = entityManager.find(MyEntity.class, key, LockModeType.PESSIMISTIC_WRITE);
  </code></pre>
  
  <p>The question is about skipping the cache and not about locking. </p>