title: CrudRepository save() throws "Row was updated or deleted by another transaction"
body: <p>I have an error during an update transaction on my database using JpaRepository. I saw that there are many questions about this same error here but none of the solutions mentioned seem to solve my case. This is my main method:</p>
  <pre><code> public View update(Long productCode, Long customerCode, UpdateForm form) {
  
          var entityToUpdate = repository.findByProductCodeAndCustomerCode(productCode, customerCode);
  
          var updatedObject = new Builder(entityToUpdate).build(form);
          var savedObject = repository.save(updatedObject); //error thrown here
  
          return converter.convertToView(savedObject);
      }
  </code></pre>
  <p>This is the Builder class implementation that appears above:</p>
  <pre><code>    public class Builder {
  
      private final Purchase entityToUpdate;
  
      public Builder(Optional&lt;Purchase&gt; entityToUpdate) {
          this.entityToUpdate = entityToUpdate.isPresent() ? entityToUpdate.get() : null;
      }
  
      public Purchase build(PurchaseUpdateForm form) {
  
          this.entityToUpdate.setDiscount(form.getDiscount());
          this.entityToUpdate.getId().setPurchaseBoxQuantity(form.getPurchaseBoxQuantity());
  
          return entityToUpdate;
      }
  }
  </code></pre>
  <p>I tried to put an <code>@Transactional</code> in the <code>update()</code> and <code>build()</code> methods but the error persists. I also tried using <code>saveAndFlush</code> but nothing changed. Full log message:</p>
  <pre><code>Optimistic locking failed; nested exception is caused by: org.hibernate.StaleObjectStateException: 
  Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): 
  [PurchasePK@bb7039e8]
  </code></pre>
  <p><strong># Additional Information - Domain</strong></p>
  <pre><code>@Data
  @Builder
  @NoArgsConstructor
  @AllArgsConstructor
  @Entity
  @Table(name = &quot;TB_PURCHASE&quot;)
  public class Purchase {
      
      @EmbeddedId
      private PurchasePK id;
      
      @NotNull
      @Column(name = &quot;DISCOUNT&quot;)
      private BigDecimal discount;
  }
  
  @Getter
  @Setter
  @Builder
  @NoArgsConstructor
  @AllArgsConstructor
  @EqualsAndHashCode(of = { &quot;product&quot;, &quot;purchaseBoxQuantity&quot; })
  @Embeddable
  public class PurchasePK {
      
      @NotNull
      @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
      @JoinColumn(name = &quot;CD_PRODUCT&quot;)
      @JoinColumn(name = &quot;CD_CUSTOMER&quot;)
      private Product product;
      
      @NotNull
      @Column(name = &quot;QT_BOXES&quot;)
      private Long purchaseBoxQuantity;
  }
  </code></pre>