title: OptimisticLockException while deleting a set of entities concurrently
body: <p>I am trying to create a simple Servlet method to delete a set of objects by using Hibernate. </p>
  
  <pre><code>List&lt;MyEntity&gt; entities = dao.getEntities(...); // just a query that returns a collection of managed entities
  
  for (MyEntity entity : entities)
    dao.remove(entity); // em.remove(..) nothing more
  </code></pre>
  
  <p>Everything works as expected but... if I use jMeter and query my fantastic Servlet 200 times in a period of a few seconds then I may receive exceptions like this one below.</p>
  
  <p>I would not be happy to use "delete from MyEntity..." update query because I also use a second level cache.</p>
  
  <p>Exception:</p>
  
  <pre><code>[INFO] Caused by: javax.persistence.RollbackException: Error while committing the transaction
  [INFO]  at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:94)
  [INFO]  at xxx.xxx.xxx.TransactionTemplate.invokeWithinTransation(TransactionTemplate.java:23)
  [INFO]  ... 42 more
  [INFO] Caused by: javax.persistence.OptimisticLockException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
  [INFO]  at org.hibernate.jpa.spi.AbstractEntityManagerImpl.wrapStaleStateException(AbstractEntityManagerImpl.java:1800)
  [INFO]  at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1705)
  [INFO]  at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677)
  [INFO]  at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:82)
  [INFO]  ... 43 more
  [INFO] Caused by: org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
  [INFO]  at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:81)
  [INFO]  at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:73)
  [INFO]  at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:63)
  [INFO]  at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3400)
  [INFO]  at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3630)
  [INFO]  at org.hibernate.action.internal.EntityDeleteAction.execute(EntityDeleteAction.java:114)
  [INFO]  at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:463)
  [INFO]  at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:349)
  [INFO]  at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)
  [INFO]  at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)
  [INFO]  at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1222)
  [INFO]  at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425)
  [INFO]  at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
  [INFO]  at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177)
  [INFO]  at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:77)
  [INFO]  ... 43 more
  </code></pre>