{"record":{"id":"9fd46d42a870b073","repo":"apache/flink","slug":"the-field-is-already-contained-in-the-hierarchy","errorCode":null,"errorMessage":"The field {} is already contained in the hierarchy of the {}.Please use unique field names through your classes hierarchy","messagePattern":"The field (.+?) is already contained in the hierarchy of the (.+?)\\.Please use unique field names through your classes hierarchy","errorType":"validation","errorClass":"InvalidTypesException","httpStatus":null,"severity":"error","filePath":"flink-core/src/main/java/org/apache/flink/api/java/typeutils/TypeExtractor.java","lineNumber":2304,"sourceCode":"     * @param ignoreDuplicates if true, in case of duplicate field names only the lowest one in a\n     *     hierarchy will be returned; throws an exception otherwise\n     * @return list of fields\n     */\n    @PublicEvolving\n    public static List<Field> getAllDeclaredFields(Class<?> clazz, boolean ignoreDuplicates) {\n        List<Field> result = new ArrayList<>();\n        while (clazz != null) {\n            Field[] fields = clazz.getDeclaredFields();\n            for (Field field : fields) {\n                if (Modifier.isTransient(field.getModifiers())\n                        || Modifier.isStatic(field.getModifiers())) {\n                    continue; // we have no use for transient or static fields\n                }\n                if (hasFieldWithSameName(field.getName(), result)) {\n                    if (ignoreDuplicates) {\n                        continue;\n                    } else {\n                        throw new InvalidTypesException(\n                                \"The field \"\n                                        + field\n                                        + \" is already contained in the hierarchy of the \"\n                                        + clazz\n                                        + \".\"\n                                        + \"Please use unique field names through your classes hierarchy\");\n                    }\n                }\n                result.add(field);\n            }\n            clazz = clazz.getSuperclass();\n        }\n        return result;\n    }\n\n    @PublicEvolving\n    public static Field getDeclaredField(Class<?> clazz, String name) {\n        for (Field field : getAllDeclaredFields(clazz, true)) {","sourceCodeStart":2286,"sourceCodeEnd":2322,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/api/java/typeutils/TypeExtractor.java#L2286-L2322","documentation":"Thrown by TypeExtractor.getAllDeclaredFields when traversing a class hierarchy and encountering two non-static, non-transient fields with the same name in different levels of the hierarchy (e.g., a field declared in both a class and its superclass). Flink requires unique field names across the entire POJO hierarchy to avoid ambiguity during serialization and field access.","triggerScenarios":"Called during getAllDeclaredFields(clazz, false) when a class hierarchy contains two or more fields with identical names (e.g., a field 'id' in both a subclass and its parent class), and ignoreDuplicates is false. This is triggered during POJO analysis when the type extractor walks the class hierarchy.","commonSituations":"A POJO subclass that redeclares a field already present in its superclass (field shadowing). Lombok-generated fields that collide with manually declared parent fields. JPA entity hierarchies where @Id fields are redeclared. Using third-party base classes with fields that conflict with your subclass fields.","solutions":["Rename the field in the subclass or superclass so all field names are unique across the hierarchy.","Remove the duplicate field declaration in the subclass if the parent field is sufficient.","If you cannot change the class hierarchy, consider using getAllDeclaredFields(clazz, true) to ignore duplicates (lowest wins), or annotate with @TypeInfo to provide custom type info.","Mark the duplicate field as transient or static if it should not participate in serialization."],"exampleFix":"// before\nclass Base {\n    private String name;\n}\nclass Child extends Base {\n    private String name; // duplicate!\n}\n\n// after\nclass Base {\n    private String name;\n}\nclass Child extends Base {\n    private String childName; // unique\n}","handlingStrategy":"validation","validationCode":"// Before type extraction, check for duplicate field names\nList<Field> allFields = getAllDeclaredFields(clazz, true); // ignore dups to probe\nSet<String> names = new HashSet<>();\nfor (Field f : allFields) {\n    if (!names.add(f.getName())) {\n    // duplicate field name detected across hierarchy\n    }\n}","typeGuard":"static boolean hasUniqueFieldNames(Class<?> clazz) {\n    Set<String> seen = new HashSet<>();\n    Class<?> c = clazz;\n    while (c != null) {\n        for (Field f : c.getDeclaredFields()) {\n            if (Modifier.isTransient(f.getModifiers()) || Modifier.isStatic(f.getModifiers())) continue;\n            if (!seen.add(f.getName())) return false;\n        }\n        c = c.getSuperclass();\n    }\n    return true;\n}","tryCatchPattern":"try {\n    List<Field> fields = getAllDeclaredFields(clazz, false);\n} catch (InvalidTypesException e) {\n    // fall back: ignore duplicates (lowest in hierarchy wins)\n    fields = getAllDeclaredFields(clazz, true);\n}","preventionTips":["Avoid field shadowing: never redeclare a field name that exists in a superclass.","Use unique field names across the entire class hierarchy.","Mark non-serializable duplicate fields as transient or static.","Consider composition over inheritance to avoid field name collisions."],"tags":["pojo","field-shadowing","class-hierarchy","type-extraction"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}