在进行jpa更新操作中报异常Cascade="All-Delete-Orphan" 处理

报错异常:A collection with cascade=“all-delete-orphan” was no longer referenced by the owning entity instance

#解决方式:

    //一对多配置  一个订单多个明细  最强级联、mappedBy = "requestId",
    @OneToMany(cascade = CascadeType.ALL,mappedBy = "requestId",orphanRemoval = true)
//    @JoinColumn(name = "request_id") //配置了mappedBy,就不能在这@JoinColumn
    private List repairDetailList=new ArrayList<>();


    public List getRepairDetailList() {
        return repairDetailList;
    }

    public void setRepairDetailList(List repairDetailList) {
    	//-----------在set方法处处理解决-----------------------
        this.repairDetailList.clear();
        this.repairDetailList.addAll(repairDetailList);
//        this.repairDetailList = repairDetailList;
    }

具体解释:

引用此处:https://blog.csdn.net/qq_17625317/article/details/52182511

最后要一方能找到多方,同样,多方也能找到一方

 @Log("修改RepairRequest")
 @ApiOperation(value = "修改RepairRequest")
  @PutMapping(value = "/repairRequest")
  @PreAuthorize("hasAnyRole('ADMIN','REPAIRREQUEST_ALL','REPAIRREQUEST_EDIT')")
  public ResponseEntity update(@Validated @RequestBody RepairRequest repairRequest){
      System.out.println("修改时前台提交过来的参数"+repairRequest);

      //一方找多方,能找到
      List repairDetailList = repairRequest.getRepairDetailList();
      System.out.println("一方找多方:"+repairDetailList);//能找到明细  但是多方中一方的关联字段为空
      //多方找一方,需要处理:将订单repairRequest设置进去就行
      for(RepairDetailed detailList :repairDetailList){
          System.out.println("多方找一方:"+detailList.getRequestId());
          detailList.setRequestId(repairRequest);
      }
      repairRequestService.update(repairRequest);
      return new ResponseEntity(HttpStatus.NO_CONTENT);
  }

在进行jpa更新操作中报异常Cascade=

而相应的实体类配置:

订单明细实体类

在进行jpa更新操作中报异常Cascade=

订单实体类

在进行jpa更新操作中报异常Cascade=

你可能感兴趣的:(JPA异常处理)