如何在没有foreigin键约束失败的情况下将新记录插入到mysql数据库中。 [英] How can I insert new records into mysql database without foreigin key constraint fail..
问题描述
我插入了第一条记录而没有错误。在第一次记录之后,我遇到了如下错误。
无法添加或更新子行:外键约束失败(`cims` .hra_prm_claim_common`, CONSTRAINT`hra_prm_claim_common_applicationId` FOREIGN KEY(`applicationId`)REFERENCES`hra_prm_claim_application`(`applicationId`)ON DELETE CASCADE ON UPDAT);嵌套异常是com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:无法添加或更新子行:外键约束失败(`cims` .hra_prm_claim_common`,CONSTRAINT`hra_prm_claim_common_applicationId` FOREIGN KEY(`applicationId`)REFERENCES`hra_prm_claim_application`(`applicationId`)ON UPETE CASCADE ON UPDAT)
我有三个表:1个主表和2个子表。对于主表,插入数据是可以的。对于子表,我面临这个错误。我插入两个子表的代码如下..
谢谢大家。
I inserted first record without error. After first record, I faced the error as follow.
"Cannot add or update a child row: a foreign key constraint fails (`cims`.`hra_prm_claim_common`, CONSTRAINT `hra_prm_claim_common_applicationId` FOREIGN KEY (`applicationId`) REFERENCES `hra_prm_claim_application` (`applicationId`) ON DELETE CASCADE ON UPDAT); nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`cims`.`hra_prm_claim_common`, CONSTRAINT `hra_prm_claim_common_applicationId` FOREIGN KEY (`applicationId`) REFERENCES `hra_prm_claim_application` (`applicationId`) ON DELETE CASCADE ON UPDAT)"
I have three tables : 1 main table and 2 sub tables. for main table, insert data is ok. for sub tables, i face this error. my code for inserting two sub tables are as below..
Thank you all.
SimpleDateFormat timeSDF = new SimpleDateFormat(propertiesConstants.getSimpleDateFormat());
HraPrmClaimCommon commonInsert = new HraPrmClaimCommon();
HraPrmClaimTransportation transportationInsert = new HraPrmClaimTransportation();
for(int i=0; i<uiPrmClaimTransportation.getTransportationRecordList().size(); i++) {
commonInsert.setApplicationId(applicationId);
commonInsert.setSeqNo(i+1);
commonInsert.setStatus(propertiesConstants.getHraPrmClaimStatusCodePending());
commonInsert.setTransactionDate(timeSDF.parse(uiPrmClaimTransportation.getTransportationRecordList().get(i).getTranscationDateInString()));
commonInsert.setType(uiPrmClaimTransportation.getTransportationRecordList().get(i).getType());
commonInsert.setAmount(new BigDecimal(Double.parseDouble(uiPrmClaimTransportation.getTransportationRecordList().get(i).getAmountInString())));
if (uiPrmClaimTransportation.getTransportationRecordList().get(i).getUploadFile() != null && uiPrmClaimTransportation.getTransportationRecordList().get(i).getUploadFile().getSize() > 0) {
if (!uiPrmClaimTransportation.getTransportationRecordList().get(i).getUploadFile().getOriginalFilename().toLowerCase().endsWith(propertiesConstants.getHraFileUploadExtension()))
throw new CoreKnownException(Constants.ERROR_CODE_ER0047, Constants.ERROR_CODE_ER0047);
commonInsert.setAttachedBlob(new SerialBlob(uiPrmClaimTransportation.getTransportationRecordList().get(i).getUploadFile().getBytes()));
}
hraPrmClaimCommonDao.insert(commonInsert);
transportationInsert.setApplicationId(applicationId);
transportationInsert.setSeqNo(i+1);
transportationInsert.setStartLocation(uiPrmClaimTransportation.getTransportationRecordList().get(i).getStartLocation());
transportationInsert.setEndLocation(uiPrmClaimTransportation.getTransportationRecordList().get(i).getEndLocation());
transportationInsert.setReason(uiPrmClaimTransportation.getTransportationRecordList().get(i).getReason());
hraPrmClaimTransportationDao.insert(transportationInsert);
}
<pre lang="java">
推荐答案
根据错误,您尝试添加到表中的记录hra_prm_claim_common
在hra_prm_claim_application $ c $中不存在的
applicationId
字段中的值c>插入时applicationId
字段中的表格。
几件事要检查:
- 你是按照正确的顺序做事,首先添加父行然后添加子行
- 如果applicationid
来自数据库,你获取值
- 如果你赋值,你在两个插入中使用相同的值
- 你在同一个事务或事务中运行两个插入对于父表插入已经提交
Based on the error the record you're trying to add to tablehra_prm_claim_common
has a value inapplicationId
field that does not exist inhra_prm_claim_application
table inapplicationId
field at the time of insertion.
Few things to check:
- are you doing things in the right order, first add parent row then children
- if theapplicationid
comes from the database, do you fetch the value
- if you assign the value, do you use the same value in both inserts
- that you run both inserts in the same transaction or the transaction for the parent table insert has already been committed
这篇关于如何在没有foreigin键约束失败的情况下将新记录插入到mysql数据库中。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!