JpaRepository无法更新实体

时间:2019-07-04 18:20:02

标签: java mysql spring spring-boot jpa

据我了解,JpaRepository save()方法应该更新数据库中的实体。

我在MySQL中有一个名为REF_PERIOD的表,该表只有两列:code_id(即INT和主键)和code_name(即VARCHAR)。

这是我的实体:

@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;
}

我的存储库:

@Repository
public interface PayRefPeriodRepository extends JpaRepository<PayRefPeriod, Integer> {

}

和控制器:

@Slf4j
@RestController
@RequestMapping("/api")
public class PayRefPeriodController {

    @Autowired
    private PayRefPeriodRepository payRefPeriodRepository;

    // Get all pay reference periods
    @GetMapping("/payrefperiods")
    public List<PayRefPeriod> 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(() -> 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(() -> 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<?> deletePayRefPeriod(@PathVariable(value = "id") int payRefPeriodId) {
        PayRefPeriod payRefPeriod = payRefPeriodRepository.findById(payRefPeriodId)
                .orElseThrow(() -> new ResourceNotFoundException("PayRefPeriod", "id", payRefPeriodId));

        payRefPeriodRepository.delete(payRefPeriod);

        return ResponseEntity.ok().build();
    }
}

我希望updatePayRefPeriod方法更新我的实体(MySQL数据库中的ID为0),但是它会引发异常:java.sql.SQLIntegrityConstraintViolationException:密钥“ PRIMARY”的条目“ 0”重复。

我做错了什么?

2 个答案:

答案 0 :(得分:2)

我想这可能是特定于mysql驱动程序的限制,当我们使用Java原语类型int作为标识符时,它会受到限制。我建议您将codeId的类型更改为其装箱的对象类型Integer

@Id
@Column(name = "code_id")
private Integer codeId;

因此,底层休眠在生成相应的更新查询时可以理解 null 标识符和非null而是 0 值标识符之间的区别。

答案 1 :(得分:-1)

尝试将sequenceGenerator和GeneratedValue添加到您的id列。

@Data
@Entity
@Table(name = "REF_PERIOD")
public class PayRefPeriod {

@SequenceGenerator(name = "generator", sequenceName = "REF_PERIOD_ID_SEQ")
@GeneratedValue(strategy = GenerationType.AUTO, generator = "generator")
@Id
@Column(name = "code_id")
private int codeId;

@Column(name = "code_name", length = 254, nullable = false)
private String codeName;

}