{"record":{"id":"12f13ce37b0ca2f3","repo":"hibernate/hibernate-orm","slug":"no-singular-attribute-named-and-of-type","errorCode":null,"errorMessage":"No singular attribute named '{}' and of type '{}' in type '{}'","messagePattern":"No singular attribute named '(.+?)' and of type '(.+?)' in type '(.+?)'","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/AbstractManagedType.java","lineNumber":332,"sourceCode":"\n\t@Override\n\t@Nullable\n\tpublic SqmSingularPersistentAttribute<J, ?> findDeclaredSingularAttribute(@Nonnull String name) {\n\t\treturn declaredSingularAttributes.get( name );\n\t}\n\n\t@Override\n\t@Nonnull\n\tpublic <Y> SingularPersistentAttribute<J, Y> getDeclaredSingularAttribute(@Nonnull String name, @Nonnull Class<Y> javaType) {\n\t\treturn checkTypeForSingleAttribute( findDeclaredSingularAttribute( name ), name, javaType );\n\t}\n\n\tprivate <K,Y> SqmSingularPersistentAttribute<K,Y> checkTypeForSingleAttribute(\n\t\t\tSqmSingularPersistentAttribute<K,?> attribute,\n\t\t\tString name,\n\t\t\tClass<Y> javaType) {\n\t\tif ( attribute == null || !hasMatchingReturnType( attribute, javaType ) ) {\n\t\t\tthrow new IllegalArgumentException(\n\t\t\t\t\t\"No singular attribute named '\" + name\n\t\t\t\t\t+ ( javaType != null ? \"' and of type '\" + javaType.getName() : \"\" )\n\t\t\t\t\t+ \"' in type '\" + hibernateTypeName + \"'\"\n\t\t\t);\n\t\t}\n\t\telse {\n\t\t\t@SuppressWarnings(\"unchecked\")\n\t\t\tfinal SqmSingularPersistentAttribute<K, Y> narrowed =\n\t\t\t\t\t(SqmSingularPersistentAttribute<K, Y>) attribute;\n\t\t\treturn narrowed;\n\t\t}\n\t}\n\n\tprivate <T, Y> boolean hasMatchingReturnType(SingularAttribute<T, ?> attribute, Class<Y> javaType) {\n\t\treturn javaType == null\n\t\t\t|| attribute.getJavaType().equals( javaType )\n\t\t\t|| isPrimitiveVariant( attribute, javaType );\n\t}","sourceCodeStart":314,"sourceCodeEnd":350,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/metamodel/model/domain/internal/AbstractManagedType.java#L314-L350","documentation":"getDeclaredSingularAttribute(name, Class) failed because either no singular attribute with that name exists on the type, or one exists but its Java type does not match the requested Class (hasMatchingReturnType). The message includes the requested type only when non-null, so 'of type null' means the lookup by name itself failed.","triggerScenarios":"Calling getDeclaredSingularAttribute(\"name\", Integer.class) when 'name' is a String or is a plural/collection attribute; requesting an inherited attribute through the declared variant; misspelled name.","commonSituations":"Refactoring an attribute's type (e.g. Integer -> Long) while callers still request the old wrapper. Attempting to read a List/Set/Map attribute through the singular API. Base-class attributes with the declared variant.","solutions":["Verify the attribute exists and its exact Java type: findDeclaredSingularAttribute(name) then inspect getJavaType()","Use the untyped overload or CollectionAttribute APIs for plural attributes","Use getId-style matching with the actual type from attribute.getJavaType() rather than a hard-coded class"],"exampleFix":"// before\nvar a = productType.getDeclaredSingularAttribute(\"tags\", String.class); // tags is a Set -> throws\n\n// after\nvar a = productType.getDeclaredPluralAttribute... // if plural\n// or for singular: type matches attribute.getJavaType()","handlingStrategy":"validation","validationCode":"// Look up untyped first, then verify the Java type\nvar attr = managedType.getDeclaredSingularAttribute(name); // untyped, null-safe? throws if missing\n// safer:\nfor (Attribute<?,?> a : managedType.getAttributes()) {\n    if (a.getName().equals(name) && a instanceof SingularAttribute<?,?> sa\n            && expected.isAssignableFrom(sa.getJavaType())) { /* safe to use */ }\n}","typeGuard":"static boolean isSingularOf(ManagedType<?> type, String name, Class<?> expected) {\n    for (Attribute<?,?> a : type.getAttributes()) {\n        if (a.getName().equals(name)\n                && a instanceof SingularAttribute<?,?> sa\n                && expected.isAssignableFrom(sa.getAttributeJavaType())) return true;\n    }\n    return false;\n}","tryCatchPattern":"try {\n    return type.getDeclaredSingularAttribute(name, cls);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage() != null && e.getMessage().startsWith(\"No singular attribute named\")) {\n        // name wrong, or type mismatch, or attribute is plural\n        throw new NoSuchFieldException(name + \" on \" + type.getTypeName());\n    }\n    throw e;\n}","preventionTips":["Inspect the real Java type via findDeclaredSingularAttribute(name) before the typed call","Keep typed metamodel calls in one layer so type refactors surface immediately","Remember plural attributes fail the singular lookup — choose the right API"],"tags":["hibernate","jpa-metamodel","attribute-lookup","type-mismatch","singular-attribute"],"backgroundTag":"jpa-metamodel-attribute-not-found","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}