title: Spring Data JPA: Delete Optimistic Locking semantics
body: <p>There's an entity <code>Foo</code> with a <code>@Version</code> column. If I want to delete it I expect Spring Data JPA and/or Hibernate to check whether the current value of the <code>@Version</code> column matches the one in the database. If it does not, the deletion should be rejected. This works as expected with a detached entity:</p>
  
  <pre><code>@Transactional
  public void delete(Foo foo) {
      fooRepository.delete(foo); // throws ObjectOptimisticLockingFailureException
  }
  </code></pre>
  
  <p>But if I load the entity first from the repository and then delete it within the same transaction using a different version the deletion passes regardless of the value of <code>@Version</code> column:</p>
  
  <pre><code>@Transactional
  public void delete(int fooId, long version) {
      Foo foo = fooRepository.findOne(fooId);
      foo.setVersion(version);
      fooRepository.delete(foo); // passes regardless of value of version
  }
  </code></pre>
  
  <p>When I look into the Hibernate debug output, the version comparison is performed (<code>delete from foo where id=? and version=?</code>) but not with the effect I'm expecting.</p>
  
  <p>What am I missing?</p>