title: Spring JPA merge - ObjectOptimisticLockingFailureException
body: <h2>Spring Data JPA Merge Failure - ObjectOptimisticLockingFailureException</h2>
  
  <p>I am trying to save a entity to the DB,The entity has a composite Primary key</p>
  
  <ol>
  <li>Unique Identifer Number</li>
  <li>Date</li>
  <li>A boolean flag </li>
  </ol>
  
  <p>I have a restful API built to serve the above mentioned functionality.</p>
  
  <p>My JPA Repository interfaces and classes:</p>
  
  <pre><code>@NoRepositoryBean
  public interface customRepository&lt;T, ID extends Serializable&gt; extends JpaRepository&lt;T, ID&gt; {
    void refresh(T t);
  
    void merge(T t);
  
  }
  </code></pre>
  
  <hr>
  
  <pre><code>public class customRepositoryImpl&lt;T, ID extends Serializable&gt; extends SimpleJpaRepository&lt;T, ID&gt;
      implements customRepository&lt;T, ID&gt; {
  
    private final EntityManager entityManager;
  
    public customRepositoryImpl(JpaEntityInformation entityInformation, EntityManager entityManager) {
      super(entityInformation, entityManager);
      this.entityManager = entityManager;
    }
  
    @Override
    @Transactional
    public void refresh(T t) {
      entityManager.refresh(t);
    }
  
    @Override
    @Transactional
    public void merge(T t) {
      entityManager.merge(t);
    }
  </code></pre>
  
  <p>I am trying to merge the entity in my service class using:</p>
  
  <pre><code> myEntities.forEach(myEntity -&gt; {
            customRepository.merge(myEntity);
          });
  </code></pre>
  
  <p>When I make the API call to save the entities, I am seeing the below mentioned exception.</p>
  
  <hr>
  
  <pre><code>    "stack_trace":"org.springframework.orm.ObjectOptimisticLockingFailureException: 
  Batch update returned unexpected row count from update [0]; 
  actual row count: 0; expected: 1; 
  nested exception is org.hibernate.StaleStateException: 
  Batch update returned unexpected row count from update [0]; 
  actual row count: 0; expected: 1
  </code></pre>
  
  <hr>
  
  <p>Does jpa's merge uses batch update for persisting an entity? 
  And since I am making multiple <code>merge()</code> calls, by iterating over the entities, Does the transaction created for each <code>merge()</code> call is abruptly ending before the next <code>merge()</code> call?</p>