工作中的bug:for循环里面的创建对象和for循环外面创建对象

写这个博客的作用主要是给自己涨涨记性,以后像这种低级的错误最好不要再犯。

先看一下报错的日志:

1 2019-12-25 18:12:58.197 DEBUG 12332 --- [io-8080-exec-10] com.sinosoft.utility.SQLString           : 进入更新后的InsPart
2 2019-12-25 18:12:58.197 DEBUG 12332 --- [io-8080-exec-10] com.sinosoft.utility.SQLString           : mSql: insert into LPEdorItem ( EdorAcceptNo,EdorNo,EdorCode,PolicyNo,CustomerID,EdorState,ChangeMoney,BillMoney,EdorValiDate,Creator,Operator,MakeDate,MakeTime,ModifyDate,ModifyTime ) values ( 'MGU201912461201912358','EN20191225026','RE','MGU201912461','000000','10',1000.0,0.0,'2019-12-25','001','001','2019-12-25','18:12:41','2019-12-25','18:12:41' )
3 2019-12-25 18:12:58.246 DEBUG 12332 --- [io-8080-exec-10] com.sinosoft.utility.SQLString           : mSql: delete from LPDebitNote where EdorNo='EN20191225026' and DebitNoteNo='DN0000000234'
4 2019-12-25 18:12:58.246 DEBUG 12332 --- [io-8080-exec-10] com.sinosoft.utility.SQLString           : mSql: delete from LPDebitNote where EdorNo='EN20191225026' and DebitNoteNo='DN0000000234'
5 2019-12-25 18:12:58.372 DEBUG 12332 --- [io-8080-exec-10] com.sinosoft.utility.SQLString           : 进入更新后的InsPart
6 2019-12-25 18:12:58.372 DEBUG 12332 --- [io-8080-exec-10] com.sinosoft.utility.SQLString           : mSql: insert into LPDebitNote ( EdorNo,EdorCode,DebitNoteNo,DNTAmount,DebitNoteTitle,PaidAmnt,PaidCurr,PayDate,Status,VerificationAnt,DebitNoteMode,SubInsurerId,BankAccId,RiskCode,Creator,Operator,MakeDate,MakeTime,ModifyDate,ModifyTime ) values ( 'EN20191225026','RE','DN0000000234',1000.0,'22',1000.0,'01','2020-01-24','01',0.0,'04','ZCB-003-111','888888','X151','001','001','2019-12-25','18:12:41','2019-12-25','18:12:41' )
7 2019-12-25 18:12:58.372 DEBUG 12332 --- [io-8080-exec-10] com.sinosoft.utility.SQLString           : 进入更新后的InsPart
8 2019-12-25 18:12:58.372 DEBUG 12332 --- [io-8080-exec-10] com.sinosoft.utility.SQLString           : mSql: insert into LPDebitNote ( EdorNo,EdorCode,DebitNoteNo,DNTAmount,DebitNoteTitle,PaidAmnt,PaidCurr,PayDate,Status,VerificationAnt,DebitNoteMode,SubInsurerId,BankAccId,RiskCode,Creator,Operator,MakeDate,MakeTime,ModifyDate,ModifyTime )
 values ( 'EN20191225026','RE','DN0000000234',1000.0,'22',1000.0,'01','2020-01-24','01',0.0,'04','ZCB-003-111','888888','X151','001','001','2019-12-25','18:12:41','2019-12-25','18:12:41' )

从报错日志上面看,这个错误已经很明显了,在执行向LPDebitNote表中插入数据的时候报主键冲突。从SQL语句中看,确实是这样的。

在看一下自己之前写的代码:

其实在最上面还有一行:LPDebitNoteSchema tLPDebitNoteSchema = new LPDebitNoteSchema();

 

 现在不难看出是因为给mlpdebitnotese集合中添加了两个相同的tLPDebitNoteSchema 对象,而且都是最后一次循环的那个对象。

改正之后的代码:

 1             SQLwithBindVariables sqlbv = new SQLwithBindVariables();
 2             String tSql = "SELECT * FROM LCDebitNoteDet WHERE PolicyNo='?PolicyNo?' ";
 3             sqlbv = new SQLwithBindVariables();
 4             sqlbv.sql(tSql);
 5             sqlbv.put("PolicyNo", mLPEdorAppSchema.getPolicyNo());
 6             tLCDebitNoteDetSet = tLCDebitNoteDetDB.executeQuery(sqlbv);
 7             //在LCDEBITNOTEDET表里面一个保单号能有多个DEBITNOTENO
 8             if(tLCDebitNoteDetSet!=null&&tLCDebitNoteDetSet.size()>0){
 9                 for (int i = 1; i <=tLCDebitNoteDetSet.size() ; i++) {
10                     LPDebitNoteSchema tLPDebitNoteSchema = new LPDebitNoteSchema();
11                     SQLwithBindVariables sqlbv1 = new SQLwithBindVariables();
12                     String tSql1 = "SELECT * FROM LCDebitNote WHERE DebitNoteNo='?DebitNoteNo?' ";
13                     sqlbv1 = new SQLwithBindVariables();
14                     sqlbv1.sql(tSql1);
15                     sqlbv1.put("DebitNoteNo", tLCDebitNoteDetSet.get(i).getDebitNoteNo());
16                     tLCDebitNoteSet = tLCDebitNoteDB.executeQuery(sqlbv1);
17                     if (tLCDebitNoteSet != null && tLCDebitNoteSet.size() > 0) {
18                         tLCDebitNoteSchema = tLCDebitNoteSet.get(1);
19                         this.mReflections.transFields(tLPDebitNoteSchema, tLCDebitNoteSchema);
20                         tLPDebitNoteSchema.setEdorNo(mEdorNo);
21                         tLPDebitNoteSchema.setEdorCode(mLPEdorAppSchema.getEdorCode());
22                         //   tLPDebitNoteSchema.setSelfPayOffState("02");签发的时候在进行状态的改变
23                         // tLPDebitNoteSchema.setVerificationDate(mCurrentDate);
24                         tLPDebitNoteSchema.setOperator(mG.Operator);
25                         tLPDebitNoteSchema.setMakeDate(mCurrentDate);
26                         tLPDebitNoteSchema.setMakeTime(mCurrentTime);
27                         tLPDebitNoteSchema.setModifyDate(mCurrentDate);
28                         tLPDebitNoteSchema.setModifyTime(mCurrentTime);
29                         //mMap.put(tLPDebitNoteSchema,"DELETE&INSERT");
30                     }
31                     mLPDebitNoteSet.add(tLPDebitNoteSchema);
32                 }
33                 mMap.put(mLPDebitNoteSet,"DELETE&INSERT");
34             }

这次就能完美运行了,不会报主键冲突了。

posted on 2019-12-26 09:55  ~码铃薯~  阅读(1563)  评论(0编辑  收藏  举报

导航