我有一個應該編輯的票證對象。在我的票證對象中,有引用對象的屬性(見下文)。...@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:當我觸發更新而不進行更改時,一切正常。
為什么無法使用 JPA 更改實體的引用對象?
慕田峪9158850
2023-08-04 19:05:10