title: Spring JpaRepository: delete() with subsequent save() in the same transaction
body: <p>My entity has both autogenerated primary key (id) and business key (namespace). I need to update the record by replacing the old one. So, I'm searching it by business key, delete it and save a new entity. This works if each operation in it's own transaction. But once I put all of them in the same transaction, by the time save() is executed, delete() wasn't executed yet, so I get a constraint violation.</p>
  
  <pre><code>transactionTemplate.execute(status -&gt; {
      MyEntity oldEntity = repository.findByNamespace(namespace);
      if (oldEntity != null) {
          repository.delete(oldEntity);
      }
      repository.save(newEntity);
      return null;
  });
  </code></pre>
  
  <p>I actually managed to bypass it by adding</p>
  
  <pre><code>repository.flush();
  </code></pre>
  
  <p>But I don't really get why do I need this flush().</p>