{"record":{"id":"d23199b35c36b357","repo":"paascloud/paascloud-master","slug":"error","errorCode":null,"errorMessage":"删除数据失败!","messagePattern":"删除数据失败!","errorType":"exception","errorClass":"BusinessException","httpStatus":null,"severity":"error","filePath":"paascloud-common/paascloud-common-core/src/main/java/com/paascloud/core/support/BaseService.java","lineNumber":203,"sourceCode":"\tpublic int deleteByKey(Object key) {\n\t\treturn mapper.deleteByPrimaryKey(key);\n\t}\n\n\t/**\n\t * Batch delete int.\n\t *\n\t * @param list the list\n\t *\n\t * @return the int\n\t */\n\t@Override\n\tpublic int batchDelete(List<T> list) {\n\t\tint result = 0;\n\t\tfor (T record : list) {\n\t\t\tint count = mapper.delete(record);\n\t\t\tif (count < 1) {\n\t\t\t\tlogger.error(\"删除数据失败\");\n\t\t\t\tthrow new BusinessException(\"删除数据失败!\");\n\t\t\t}\n\t\t\tresult += count;\n\t\t}\n\t\treturn result;\n\t}\n\n\t/**\n\t * Select count by example int.\n\t *\n\t * @param example the example\n\t *\n\t * @return the int\n\t */\n\t@Override\n\tpublic int selectCountByExample(Object example) {\n\t\treturn mapper.selectCountByExample(example);\n\t}\n","sourceCodeStart":185,"sourceCodeEnd":221,"githubUrl":"https://github.com/paascloud/paascloud-master/blob/781281a9503332ed3cef44ea618349d14230a127/paascloud-common/paascloud-common-core/src/main/java/com/paascloud/core/support/BaseService.java#L185-L221","documentation":"BaseService.batchDelete iterates a list and deletes each record via mapper.delete; if any single delete affects fewer than 1 row, it throws BusinessException(\"删除数据失败!\") (\"data delete failed\"). It guards against silent no-op deletes, e.g. the record no longer exists in the database.","triggerScenarios":"Calling batchDelete(list) where mapper.delete(record) returns 0 for any record — typically the row was already deleted, the primary/condition keys on the entity don't match a row, or the entity's id is null.","commonSituations":"Concurrent deletion by another request/transaction, deleting by an entity whose id was never set (mapper matches nothing), soft-delete setups where delete conditions no longer match, or stale UI data submitted twice.","solutions":["Verify the entities in the list carry valid, existing primary keys before calling batchDelete.","Refresh the list from the DB to drop rows already deleted by other sessions.","If idempotent bulk deletion is intended, catch BusinessException or switch to a single batch delete statement that tolerates missing rows."],"exampleFix":"// before\nservice.batchDelete(staleList);\n// after\nList<T> existing = mapper.selectByExample(ids);\nif (!existing.isEmpty()) {\n    service.batchDelete(existing);\n}","handlingStrategy":"try-catch","validationCode":"for (T record : list) {\n    if (record.getId() == null || mapper.selectByPrimaryKey(record.getId()) == null) {\n        throw new IllegalStateException(\"record to delete does not exist: \" + record.getId());\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    service.batchDelete(list);\n} catch (BusinessException e) {\n    log.warn(\"some rows were already deleted, refreshing list\");\n    list = reloadFromDb(list);\n}","preventionTips":["Reload entities from the DB before batch deletion to avoid stale ids.","Use optimistic locking or version columns for concurrent deletes.","Prefer a single batch delete-by-ids statement for idempotency."],"tags":["database","delete","mybatis","batch"],"backgroundTag":"database-write-failed","analyzedSha":"781281a9503332ed3cef44ea618349d14230a127","analyzedAt":"2026-09-10T10:59:02.070Z","contentChangedAt":"2026-09-10T10:59:02.070Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}