title: JpaRepository not updating entity
body: <p>As far as I undestood, JpaRepository save() method should update my entity in database.</p>
  
  <p>I have a table in MySQL called REF_PERIOD with two columns only: code_id, which is INT and Primary Key, and code_name, which is VARCHAR.</p>
  
  <p>This is my Entity:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Data
  @Entity
  @Table(name = "REF_PERIOD")
  public class PayRefPeriod {
  
      @Id
      @Column(name = "code_id")
      private int codeId;
  
      @Column(name = "code_name", length = 254, nullable = false)
      private String codeName;
  }
  </code></pre>
  
  <p>My Repository:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Repository
  public interface PayRefPeriodRepository extends JpaRepository&lt;PayRefPeriod, Integer&gt; {
  
  }
  </code></pre>
  
  <p>And the Controller:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Slf4j
  @RestController
  @RequestMapping("/api")
  public class PayRefPeriodController {
  
      @Autowired
      private PayRefPeriodRepository payRefPeriodRepository;
  
      // Get all pay reference periods
      @GetMapping("/payrefperiods")
      public List&lt;PayRefPeriod&gt; getAllPayRefPeriod() {
          return payRefPeriodRepository.findAll();
      }
  
      // Create a new pay reference period
      @PostMapping("/payrefperiods")
      public PayRefPeriod createPayRefPeriod(@Valid @RequestBody PayRefPeriod payRefPeriod) {
          return payRefPeriodRepository.save(payRefPeriod);
      }
  
      // Get a single pay reference period
      @GetMapping("/payrefperiods/{id}")
      public PayRefPeriod PayRefPeriod(@PathVariable(value = "id") int payRefPeriodId) {
          return payRefPeriodRepository.findById(payRefPeriodId)
                  .orElseThrow(() -&gt; new ResourceNotFoundException("PayRefPeriod", "id", payRefPeriodId));
      }
  
      // Update a pay reference period
      @PutMapping("/payrefperiods/{id}")
      public PayRefPeriod updatePayRefPeriod(@PathVariable(value = "id") int payRefPeriodId,
                                        @Valid @RequestBody PayRefPeriod payRefPeriodDetails) {
  
          PayRefPeriod payRefPeriod = payRefPeriodRepository.findById(payRefPeriodId)
                  .orElseThrow(() -&gt; new ResourceNotFoundException("PayRefPeriod", "id", payRefPeriodId));
  
          payRefPeriod.setCodeName(payRefPeriodDetails.getCodeName());
  
          log.debug(payRefPeriod.toString());
  
          return payRefPeriodRepository.save(payRefPeriod);
      }
  
      // Delete a pay reference period
      @DeleteMapping("/payrefperiods/{id}")
      public ResponseEntity&lt;?&gt; deletePayRefPeriod(@PathVariable(value = "id") int payRefPeriodId) {
          PayRefPeriod payRefPeriod = payRefPeriodRepository.findById(payRefPeriodId)
                  .orElseThrow(() -&gt; new ResourceNotFoundException("PayRefPeriod", "id", payRefPeriodId));
  
          payRefPeriodRepository.delete(payRefPeriod);
  
          return ResponseEntity.ok().build();
      }
  }
  </code></pre>
  
  <p>I expect the updatePayRefPeriod method to update my entity (which has Id 0 in MySQL database), however it throws an exception: java.sql.SQLIntegrityConstraintViolationException: Duplicate entry '0' for key 'PRIMARY'.</p>
  
  <p>What I'm doing wrong?</p>