{"record":{"id":"0f3939023082652c","repo":"jeecgboot/JeecgBoot","slug":"parentcode-0f3939","errorCode":null,"errorMessage":"该编码【${parentCode}】存在多个，请核实!","messagePattern":"该编码【(.+?)】存在多个，请核实!","errorType":"exception","errorClass":"JeecgBootException","httpStatus":null,"severity":"error","filePath":"jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/resources/jeecg/code-template-online/default/tree/java/${bussiPackage}/${entityPackage}/service/impl/${entityName}ServiceImpl.javai","lineNumber":145,"sourceCode":"                    mapList.add(data);\n                }\n            }\n        }\n        return mapList;\n    }\n\n    @Override\n    public List<SelectTreeModel> queryListByCode(String parentCode) {\n        String pid = ROOT_PID_VALUE;\n        if (oConvertUtils.isNotEmpty(parentCode)) {\n            LambdaQueryWrapper<${entityName}> queryWrapper = new LambdaQueryWrapper<>();\n            queryWrapper.eq(${entityName}::get${pidFieldName?cap_first}, parentCode);\n            List<${entityName}> list = baseMapper.selectList(queryWrapper);\n            if (list == null || list.size() == 0) {\n                throw new JeecgBootException(\"该编码【\" + parentCode + \"】不存在，请核实!\");\n            }\n            if (list.size() > 1) {\n                throw new JeecgBootException(\"该编码【\" + parentCode + \"】存在多个，请核实!\");\n            }\n            pid = list.get(0).getId();\n        }\n        return baseMapper.queryListByPid(pid, null);\n    }\n\n    @Override\n    public List<SelectTreeModel> queryListByPid(String pid) {\n        if (oConvertUtils.isEmpty(pid)) {\n            pid = ROOT_PID_VALUE;\n        }\n        return baseMapper.queryListByPid(pid, null);\n    }\n\n\t/**\n\t * 根据所传pid查询旧的父级节点的子节点并修改相应状态值\n\t * @param pid\n\t */","sourceCodeStart":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/jeecgboot/JeecgBoot/blob/96fb33f5ec68516da0b0147da06b2eb0419e063a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/resources/jeecg/code-template-online/default/tree/java/${bussiPackage}/${entityPackage}/service/impl/${entityName}ServiceImpl.javai#L127-L163","documentation":"This is a FreeMarker code-generator template (note the `.javai` extension) that emits a `queryListByCode` service method for online tree-list entities. At runtime the generated method looks up the parent node by `parentCode` against the configured parent-id field `${pidFieldName}`; if that lookup returns MORE than one row it throws `JeecgBootException` because the parent code is ambiguous. The guard exists so the tree never silently picks an arbitrary parent.","triggerScenarios":"Calling the generated `queryListByCode(parentCode)` with a non-empty `parentCode` whose value occurs in MORE than one row of the `${pidFieldName}` column (e.g. two records share `parent_code = 'A01'`). Only fires when `parentCode` is non-empty; an empty/null `parentCode` falls back to `ROOT_PID_VALUE` and never throws.","commonSituations":"Seed/import data loaded duplicate parent codes; the cgform tree settings point `pidFieldName` at a non-unique display column (a name) instead of a true code; rows were hand-edited to repeat a code; a unique index was never created on the parent-code column.","solutions":["Query the offending duplicates: `SELECT ${pidFieldName}, COUNT(*) FROM <table> WHERE ${pidFieldName} = '<parentCode>' GROUP BY ${pidFieldName}` and merge/delete the extras.","Confirm the cgform tree config uses a genuinely unique parent-code field, not a display name.","Add a unique DB index on the parent-code column to block future ambiguity.","If duplicates are legitimate, change the lookup to disambiguate with a second column instead of relying on `parentCode` alone."],"exampleFix":"// before\nqueryWrapper.eq(${entityName}::get${pidFieldName?cap_first}, parentCode);\nList<${entityName}> list = baseMapper.selectList(queryWrapper);\nif (list.size() > 1) { throw new JeecgBootException(\"该编码【\" + parentCode + \"】存在多个，请核实!\"); }\n\n// after — narrow with an extra discriminator + keep the guard as a safety net\nqueryWrapper.eq(${entityName}::get${pidFieldName?cap_first}, parentCode)\n            .eq(${entityName}::getTenantId, TenantContext.getTenantId())\n            .last(\"LIMIT 1\");","handlingStrategy":"validation","validationCode":"// before calling queryListByCode, verify the parent code is unique\nlong count = baseMapper.selectCount(new LambdaQueryWrapper<${entityName}>()\n    .eq(${entityName}::get${pidFieldName?cap_first}, parentCode));\nif (count > 1) {\n    // surface duplicates to the user / pick a different code; do NOT call queryListByCode\n}","typeGuard":null,"tryCatchPattern":"// at the controller boundary — JeecgBootException is a business exception\ntry {\n    return Result.OK(service.queryListByCode(parentCode));\n} catch (JeecgBootException e) {\n    return Result.error(e.getMessage());\n}","preventionTips":["Enforce a unique DB index on the parent-code column used for tree linkage.","Validate parent-code uniqueness inside the entity save/update service before insert.","Link tree parents by generated primary id, not by a mutable business code.","Cover the duplicate-parent scenario in a service unit test."],"tags":["jeecg","code-generator","tree","data-integrity","freemarker","java","online-cgform"],"backgroundTag":null,"analyzedSha":"96fb33f5ec68516da0b0147da06b2eb0419e063a","analyzedAt":"2026-08-14T00:04:16.786Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}