title: JPA @Version - Why does optimistic locking not work when attempting to update a deleted entity?
body: <p>I need to prevent a user from updating an entity that some other user has already deleted. This is the desired behavior:</p>
  
  <ol>
  <li>User A views the record @ version 0.</li>
  <li>User B views the record @ version 0.</li>
  <li>User A deletes the record.</li>
  <li>User B tries to update the record. But User A already deleted so this should fail.</li>
  </ol>
  
  <p>I'm trying to use optimistic concurrency control via @Version to accomplish this.  </p>
  
  <p>I have an versioned entity such as:</p>
  
  <pre><code>@Entity
  public class Foo {
  
      @Id
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      private Long id;
  
      @Version
      private Integer version;
  
      @OneToMany(cascade=CascadeType.ALL, orphanRemoval=true)
      @JoinColumn(name="orderId", nullable=false)
      private List&lt;Bar&gt; bars;
  </code></pre>
  
  <p><code>Bar</code> is not versioned:</p>
  
  <pre><code>@Entity
  public class Bar {
  
      @Id
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      private Long id;
  </code></pre>
  
  <p>At step 3, User A requests delete. A transaction is created and <code>delete</code> is called on the <code>FooRepository</code> which implements the <code>CrudRepository</code>:</p>
  
  <pre><code>@RestController
  public class FooController {
  
  
      private FooService fooService;
  
      @DeleteMapping
      @ResponseStatus(HttpStatus.NO_CONTENT)
      public void delete(Foo foo) {       
  
          fooService.delete(foo);
      }
  
      @PutMapping
      @ResponseStatus(HttpStatus.NO_CONTENT)
      public void update(Foo foo) {       
  
          fooService.update(foo);
      }
  
  
   @Service
   public class FooService {
  
     private FooRepository fooRepo;
  
     @Transactional
     public void delete(Foo foo) {
          fooRepo.delete(foo);
     }
  
     @Transactional
     public void update(Foo foo) {
          fooRepo.save(foo);
     }
  </code></pre>
  
  <p>At step 4 sometime after step 3, user B calls update and  </p>
  
  <p><code>fooRepo.save(foo);</code> will be called in a separate transaction</p>
  
  <p>This results in inserting a new foo row into the database?!? <code>foo</code> is provided by the client and has the same id and version that was used in the delete. Since foo has an id, I thought it would try to update and then realize it could not because the entity was deleted from the DB. Instead it ignores foo's id and inserts a new record into the DB with a different id?!?!?!?</p>
  
  <p>How can I get an <code>org.springframework.orm.ObjectOptimisticLockingFailureException</code> to be thrown instead?</p>
  
  <p>Looking at the <code>save</code> implementation <code>SimpleJpaRepository</code>, <code>entityInformation.isNew(entity)</code> should return false during update since <code>foo</code>'s id has a nonzero value. That means the entity manager should perform a merge.</p>