一、报错日志如下:
后台事务异常,AppName: standard-prod-daily-bj4001a-mservice-drp,InstanceId: standard-prod-daily-bj4001a-mservice-drp-0627244108,taskId: 1589241523222880256 ,errorInfo: kd.bos.exception.KDException: ERROR: duplicate key value violates unique constraint "t_im_otherinbill_pkey"
Detail: Key (fid)=(1589241531317926912) already exists.
RequestContext: tenantId=textilesfuture, accountId=1468730924374955008
TX: TXContext.986826:tag=im_otherinbill.save, level=1, propagation=REQUIRES_NEW, rollback=false, halt=false, ended=false
SQL: /*ORM*/ INSERT INTO t_im_otherinbill(fk_dowu_csnumber,fk_dowu_warehouse1,fk_dowu_bd_materialgroup,fcustomerid,fsettlecurrencyid,fsupplierid,flastupdateuserid,finvschemeid,fbiztypeid,fbizoperatorid,fbizoperatorgroupid,foperatorid,foperatorgroupid,fdeptid,fbizdeptid,fbizorgid,fbilltypeid,forgid,fauditorid,fmodifierid,fcreatorid,fk_dowu_jgddh1,fk_dowu_jgddh,fk_dowu_htorder,fk_dowu_htordertype,fk_dowu_supillerno,fk_dowu_salesorder,fk_dowu_relevance_out_no,fk_dowu_ifsend,fk_dowu_combofield,fk_dowu_autocomplete,fk_dowu_isinitsnnumber,funitsrctype,flastupdatetime,fischargeoff,fischargeoffed,fasyncstatus,fbookdate,fbillcretype,fisvirtualbill,fcomment,fbiztime,fcreatetime,fmodifytime,fauditdate,fbillstatus,fbillno,FId) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
二、分析
看报错可以看出是INSERT INTO t_im_otherinbill保存数据时报错,可以找到保存的代码进行分析
ConvertOperationResult pushResult = ConvertServiceHelper.push(pushArgs);
DynamicObject targetBillNew=targetBillObjs.get(0);
第一次保存 :
targetBillNew.set(key1,value1);
OperationResult againSaveResult1 = OperationServiceHelper.executeOperate("save", "im_otherinbill", new DynamicObject[]{targetBillNew}, againSaveOption);
第二次保存:
targetBillNew.set(key2,value2);
OperationResult againSaveResult1 = OperationServiceHelper.executeOperate("save", "im_otherinbill", new DynamicObject[]{targetBillObjs.get(0)}, againSaveOption);
如上代码两次对同一个DynamicObject 对象targetBillNew进行保存,导致保存时生成了同一个主键导致出现的主键重复,触发唯一性索引。
三、结果
正确做法是:第二次保存时的保存对象必须先从数据库中查出BusinessDataServiceHelper.loadSingle最新的数据包进行修改,然后才能进行第二次保存;
推荐阅读