{"record":{"id":"0b0b9531f6a7bf41","repo":"baomidou/mybatis-plus","slug":"duplicate-key","errorCode":null,"errorMessage":"Duplicate key {}","messagePattern":"Duplicate key (.+?)","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/ReflectionKit.java","lineNumber":168,"sourceCode":"                /* 过滤 transient关键字修饰的属性 */\n                .filter(f -> !Modifier.isTransient(f.getModifiers()))\n                .collect(Collectors.toList());\n        });\n    }\n\n    /**\n     * <p>\n     * 排序重置父类属性\n     * </p>\n     *\n     * @param fields         子类属性\n     * @param superFieldList 父类属性\n     */\n    public static Map<String, Field> excludeOverrideSuperField(Field[] fields, List<Field> superFieldList) {\n        // 子类属性\n        Map<String, Field> fieldMap = Stream.of(fields).collect(toMap(Field::getName, identity(),\n            (u, v) -> {\n                throw new IllegalStateException(\"Duplicate key \" + u);\n            }, LinkedHashMap::new));\n        superFieldList.stream().filter(field -> !fieldMap.containsKey(field.getName()))\n            .forEach(f -> fieldMap.put(f.getName(), f));\n        return fieldMap;\n    }\n\n    /**\n     * 判断是否为基本类型或基本包装类型\n     *\n     * @param clazz class\n     * @return 是否基本类型或基本包装类型\n     */\n    @Deprecated\n    public static boolean isPrimitiveOrWrapper(Class<?> clazz) {\n        Assert.notNull(clazz, \"Class must not be null\");\n        return (clazz.isPrimitive() || PRIMITIVE_WRAPPER_TYPE_MAP.containsKey(clazz));\n    }\n","sourceCodeStart":150,"sourceCodeEnd":186,"githubUrl":"https://github.com/baomidou/mybatis-plus/blob/bf67d907478c724120bf76292da54abf9e73c2b3/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/ReflectionKit.java#L150-L186","documentation":"ReflectionKit.excludeOverrideSuperField builds a name-to-Field map of a subclass's declared fields using Collectors.toMap with a merge function that throws IllegalStateException('Duplicate key <field>') on key collision. In Java, one class cannot declare two fields with the same name, so a duplicate here means the input Field[] contains the same field twice — i.e. a class is present multiple times in the type hierarchy traversal (often with interface/default-method or bridge situations in mybatis-plus versions of that era).","triggerScenarios":"Calling ReflectionKit.excludeOverrideSuperField(fields, superFields) (or mybatis-plus entity metadata init that calls it) where the fields array contains two Field objects with the same name — e.g. getDeclaredFields() of a class that javac duplicated fields for (visibility-bridge generics with covariant overrides), or a user supplying a hand-built Field[] containing duplicates.","commonSituations":"Entity classes with generic hierarchies where reflection sees duplicate bridge-backed fields; bugs in older mybatis-plus versions (3.4.x era) resolved in later releases; user code copying this helper and feeding it unchecked field lists.","solutions":["Upgrade mybatis-plus to the latest patch release — duplicate-field traversal bugs in excludeOverrideSuperField callers were fixed in subsequent versions.","If calling the API directly, deduplicate the Field[] by name (and declaring class) before passing it in.","Inspect the entity hierarchy: remove redundant re-declaration of a field in both a subclass and an interface/default context that triggers duplicate discovery."],"exampleFix":"// before\nMap<String, Field> map = ReflectionKit.excludeOverrideSuperField(dupFields, superFields);\n\n// after: dedupe by name first\nMap<String, Field> unique = Arrays.stream(dupFields)\n    .collect(Collectors.toMap(Field::getName, Function.identity(), (a, b) -> a, LinkedHashMap::new));\nMap<String, Field> map = ReflectionKit.excludeOverrideSuperField(unique.values().toArray(new Field[0]), superFields);","handlingStrategy":"validation","validationCode":"Set<String> seen = new HashSet<>();\nfor (Field f : fields) {\n    if (!seen.add(f.getName() + \"@\" + f.getDeclaringClass().getName())) {\n        throw new IllegalStateException(\"duplicate field in input: \" + f);\n    }\n}\nMap<String, Field> m = ReflectionKit.excludeOverrideSuperField(fields, superFields);","typeGuard":"static boolean hasNoDuplicateNames(Field[] fields) {\n    Set<String> names = new HashSet<>();\n    for (Field f : fields) if (!names.add(f.getName())) return false;\n    return true;\n}","tryCatchPattern":"try {\n    Map<String, Field> m = ReflectionKit.excludeOverrideSuperField(fields, superFields);\n} catch (IllegalStateException e) {\n    throw new IllegalStateException(\"duplicate field discovered in hierarchy — upgrade mybatis-plus\", e);\n}","preventionTips":["Deduplicate field arrays by name before reflective map building.","Keep mybatis-plus current; hierarchy-traversal duplicates were fixed in later 3.4.x/3.5.x patches."],"tags":["reflection","entity-metadata","fields","version-bug"],"backgroundTag":null,"analyzedSha":"bf67d907478c724120bf76292da54abf9e73c2b3","analyzedAt":"2026-08-14T15:17:09.543Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}