{"record":{"id":"64d476ae79928d9b","repo":"hibernate/hibernate-orm","slug":"mapattribute-named-name-does-not-support-a-key","errorCode":null,"errorMessage":"MapAttribute named ${name} does not support a key of type ${keyType}","messagePattern":"MapAttribute named (.+?) does not support a key of type (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/AbstractManagedType.java","lineNumber":678,"sourceCode":"\t@Nonnull\n\tpublic <K, V> MapAttribute<? super J, K, V> getMap(\n\t\t\t@Nonnull String name,\n\t\t\t@Nonnull Class<K> keyType,\n\t\t\t@Nonnull Class<V> valueType) {\n\t\tfinal var attribute = findPluralAttribute( name );\n\t\tcheckMapValueType( attribute, name, valueType );\n\t\tfinal var mapAttribute = (MapAttribute<? super J, K, V>) attribute;\n\t\tcheckMapKeyType( mapAttribute, name, keyType );\n\t\treturn mapAttribute;\n\t}\n\n\tprivate <V> void checkMapValueType(PluralAttribute<? super J, ?, ?> attribute, String name, Class<V> valueType) {\n\t\tcheckTypeForPluralAttributes( \"MapAttribute\", attribute, name, valueType, PluralAttribute.CollectionType.MAP);\n\t}\n\n\tprivate <K,V> void checkMapKeyType(MapAttribute<? super J, K, V> mapAttribute, String name, Class<K> keyType) {\n\t\tif ( mapAttribute.getKeyJavaType() != keyType ) {\n\t\t\tthrow new IllegalArgumentException( \"MapAttribute named \" + name + \" does not support a key of type \" + keyType );\n\t\t}\n\t}\n\n\t@Override\n\t@SuppressWarnings(\"unchecked\")\n\t@Nonnull\n\tpublic <K, V> MapAttribute<J, K, V> getDeclaredMap(\n\t\t\t@Nonnull String name,\n\t\t\t@Nonnull Class<K> keyType,\n\t\t\t@Nonnull Class<V> valueType) {\n\t\tfinal var attribute = findDeclaredPluralAttribute( name );\n\t\tcheckMapValueType( attribute, name, valueType );\n\t\tfinal var mapAttribute = (MapAttribute<J, K, V>) attribute;\n\t\tcheckMapKeyType( mapAttribute, name, keyType );\n\t\treturn mapAttribute;\n\t}\n\n","sourceCodeStart":660,"sourceCodeEnd":696,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/AbstractManagedType.java#L660-L696","documentation":"The typed map lookup getMap(name, keyType, valueType) passed the value-type check but failed the key check: mapAttribute.getKeyJavaType() != keyType uses strict Class identity (not assignability). The requested key Class must be exactly the map's declared key type.","triggerScenarios":"Calling getMap(\"labels\", Object.class, String.class) on Map<String,String>; passing a subclass (e.g. Long.class for a key actually typed as the declared Long works, but Integer vs int, or String vs CharSequence fails); primitive/wrapper mismatches in the key.","commonSituations":"Generic code passing Object.class or a widened type as the key; keys whose declared type is a primitive wrapper and the caller passes the primitive class; map keys changed during refactoring.","solutions":["Pass the exact key type: inspect mapAttribute.getKeyJavaType() (via findPluralAttribute first) and request that class","Use the untyped getMap(name) overload which skips key/value type checks","Avoid widening key types in generic helpers; branch on the real key class"],"exampleFix":"// before\nMapAttribute<Config, Object, String> m = configType.getMap(\"labels\", Object.class, String.class); // key is String -> throws\n\n// after\nMapAttribute<Config, String, String> m = configType.getMap(\"labels\", String.class, String.class);","handlingStrategy":"validation","validationCode":"// Resolve the exact key class first; the check is Class identity (==)\nvar any = type.findPluralAttribute(name);\nif (any instanceof javax.persistence.metamodel.MapAttribute<?,?,?> m\n        && m.getKeyJavaType() == keyType) {\n    return type.getMap(name, keyType, valueType);\n}\nthrow new IllegalArgumentException(\n    name + \" key type is \" + (any == null ? \"?\" : ((MapAttribute<?,?,?>) any).getKeyJavaType()));","typeGuard":null,"tryCatchPattern":"try {\n    return type.getMap(name, keyType, valueType);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"does not support a key of type\")) {\n        Class<?> realKey = type.getMap(name).getKeyJavaType();\n        return type.getMap(name, (Class) realKey, valueType);\n    }\n    throw e;\n}","preventionTips":["Never pass Object.class or widened key types — the check is exact identity","Derive the key class from getMap(name).getKeyJavaType() when unknown","Use the untyped getMap(name) overload in generic code"],"tags":["hibernate","jpa-metamodel","map-attribute","key-type","type-mismatch"],"backgroundTag":"jpa-metamodel-collection-mismatch","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}