我有一个应该编辑的票证对象。在我的票证对象中,有引用对象的属性(见下文)。
...
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "project_id", referencedColumnName="id")
private Project project;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "costCenter_id", referencedColumnName="id")
private CostCenter costCenter;
...
但是当我尝试更新实体时,我总是收到错误:
无法提交 JPA 事务;嵌套异常是 javax.persistence.RollbackException:提交事务时出错
@PutMapping("/tickets/{id}")
@ResponseStatus(HttpStatus.OK)
public Ticket updateTicket(@RequestBody Ticket ticket) throws Exception{
Optional<Ticket> o = this.ticketRepo.findById(ticket.getId());
o.ifPresent(element -> {
if(ticket.getCostCenter() != null) {
Optional<CostCenter> c = this.costCenterRepo.findById(ticket.getCostCenter().getId());
c.ifPresent( costCenter -> {
element.setCostCenter(costCenter);
});
}
if(ticket.getProject() != null) {
Optional<Project> p = this.projectRepo.findById(ticket.getProject().getId());
p.ifPresent(project -> {
element.setProject(project);
});
}
this.ticketRepo.save(element);
});
return o.orElseThrow(() -> new NotFoundException(ticket.getId()));
}
PS:当我触发更新而不进行更改时,一切正常。
桃花长相依
相关分类