我尝试在for循环中向DB添加元素。只有一个进入DB然后我得到500(内部服务器错误)重复密钥。奇怪的是,在javaScript日志中我连续有两个reasonList对象,之后它会保存并尝试保存同一个对象的两倍,最后一个。
Glassfish错误日志
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '4-3-6' for key 'PRIMARY'
在控制器中我尝试同时保存rma和reasonList
savedRma = rmaService.save({}, rma);
savedRma.$promise.then(function (result) {
$scope.rma = result;
console.log('result.rmaID--------->'+ result.rmaId);
for (var i = 0; i < $scope.reasonList.length; i++) {
$scope.rmaHasDefectdesc.rmaHasDefectdescPK.defectdescDefectdescId = reasonList[i].defectdescId;
$scope.rmaHasDefectdesc.rmaHasDefectdescPK.returnreasonId = reasonList[i].returnreasonReturnreasonId.returnreasonId;
$scope.rmaHasDefectdesc.rmaHasDefectdescPK.rmaId = $scope.rma.rmaId;
console.log('defectDescID: ' +reasonList[i].defectdescId);
console.log('returnReasonID: ' +reasonList[i].returnreasonReturnreasonId.returnreasonId)
console.log('rmaID: ' +$scope.rma.rmaId);
savedRmaHasDefectdesc = rmaDefectSvc.save({}, $scope.rmaHasDefectdesc);
savedRmaHasDefectdesc.$promise.then(function (response) {
$scope.savedRmaHasDefectdesc = response;
console.log('response.rmaHasDefectdescPK--------->'+ response.rmaHasDefectdescPK.defectdescDefectdescId);
console.log('response.rmaHasDefectdescPK.returnreasonId--------->'+ response.rmaHasDefectdescPK.returnreasonId);
console.log('response.rmaHasDefectdescPK.rmaId--------->'+ response.rmaHasDefectdescPK.rmaId);
});
} //FOR LOOP ENDS
});//savedRma.promise.then ENDS