工作中的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             }

這次就能完美運行了,不會報主鍵沖突了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM