工作中的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   ~码铃薯~  阅读(1569)  评论(0编辑  收藏  举报

编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示