{"record":{"id":"1783da3ccb4480c1","repo":"hibernate/hibernate-orm","slug":"could-not-resolve-attribute-name-of-any-mappi","errorCode":null,"errorMessage":"Could not resolve attribute '${name}' of any mapping (must be one of 'class', 'id')","messagePattern":"Could not resolve attribute '(.+?)' of any mapping \\(must be one of 'class', 'id'\\)","errorType":"exception","errorClass":"PropertyNotFoundException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/type/AnyType.java","lineNumber":402,"sourceCode":"\t}\n\n\tprivate static final String[] PROPERTY_NAMES = new String[] { \"class\", \"id\" };\n\n\t@Override\n\tpublic String[] getPropertyNames() {\n\t\treturn PROPERTY_NAMES;\n\t}\n\n\t@Override\n\tpublic int getPropertyIndex(String name) {\n\t\tif ( PROPERTY_NAMES[0].equals( name ) ) {\n\t\t\treturn 0;\n\t\t}\n\t\telse if ( PROPERTY_NAMES[1].equals( name ) ) {\n\t\t\treturn 1;\n\t\t}\n\n\t\tthrow new PropertyNotFoundException( \"Could not resolve attribute '\" + name\n\t\t\t\t+ \"' of any mapping (must be one of 'class', 'id')\" );\n\t}\n\n\t@Override\n\tpublic Object getPropertyValue(Object component, int i, SharedSessionContractImplementor session) throws HibernateException {\n\t\treturn i==0\n\t\t\t\t? session.bestGuessEntityName( component )\n\t\t\t\t: getIdentifier( component, session );\n\t}\n\n\t@Override\n\tpublic Object[] getPropertyValues(Object component, SharedSessionContractImplementor session) throws HibernateException {\n\t\treturn new Object[] {\n\t\t\t\tsession.bestGuessEntityName( component ),\n\t\t\t\tgetIdentifier( component, session )\n\t\t};\n\t}\n","sourceCodeStart":384,"sourceCodeEnd":420,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/type/AnyType.java#L384-L420","documentation":"AnyType.getPropertyIndex (AnyType.java:394) only knows the two synthetic sub-attributes of an @Any mapping: 'class' (the discriminator) and 'id' (the foreign key). Any other attribute name asked for on an any-typed path throws PropertyNotFoundException with this message, listing the two legal names.","triggerScenarios":"HQL/JPQL or Criteria that navigates into an @Any/@ManyToAny property with an attribute other than class or id, e.g. 'from Doc d where d.owner.name = :n' where owner is @Any; also programmatic calls to AnyType.getPropertyIndex(name), e.g. via property resolution APIs or reflection-based mapping validation.","commonSituations":"Developers treating @Any like a real entity association in queries; query generators that walk metamodel attributes uniformly and emit nested paths over any-typed members; refactoring a @ManyToOne to @Any and leaving old JPQL path expressions in place.","solutions":["Query only the two valid sub-attributes: compare the discriminator ('class') and the id, e.g. 'where d.owner.class = User and d.owner.id = :id' (or type(...) checks per HQL dialect support).","If you need to filter by fields of the target entity, load the target by the stored (class, id) pair in a second query or restructure to @ManyToOne so real joins are possible.","Regenerate derived queries after changing an association from @ManyToOne to @Any."],"exampleFix":"// before (owner is @Any)\nList<Doc> docs = session.createQuery(\n    \"from Doc d where d.owner.name = :n\", Doc.class)\n    .setParameter(\"n\", \"alice\")\n    .getResultList();\n\n// after: constrain via discriminator + id, then load targets\nList<Doc> docs = session.createQuery(\n    \"from Doc d where d.owner.class = User and d.owner.id = :uid\", Doc.class)\n    .setParameter(\"uid\", aliceId)\n    .getResultList();","handlingStrategy":"validation","validationCode":"// Validate an attribute path against the metamodel before running HQL\nstatic void checkPath(Metamodel mm, Class<?> root, String... path) {\n    Bindable<?> b = mm.entity(root);\n    for (String name : path) {\n        Attribute<?, ?> a = ((ManagedType<?>) b).getAttribute(name); // throws IllegalArgumentException early\n        b = (a instanceof SingularAttribute<?, ?> sa) ? sa.getType() : null;\n    }\n}\ncheckPath(mm, Doc.class, \"owner\", \"id\"); // ok for @Any; \"name\" would fail with a clear message","typeGuard":"static boolean isAnyMapped(Attribute<?, ?> attr) {\n    return attr.getJavaMember() instanceof Field f\n        && (f.isAnnotationPresent(Any.class) || f.isAnnotationPresent(ManyToAny.class));\n}","tryCatchPattern":"try {\n    return session.createQuery(jpql, Doc.class).getResultList();\n} catch (IllegalArgumentException e) {\n    if (e.getCause() instanceof PropertyNotFoundException pnfe\n            && pnfe.getMessage().contains(\"of any mapping\")) {\n        throw new QueryValidationException(\n            \"Only 'class' and 'id' are queryable on an @Any attribute: \" + jpql, e);\n    }\n    throw e;\n}","preventionTips":["Treat @Any attributes as (class, id) pairs in every query; never navigate into target fields.","When converting @ManyToOne to @Any, grep queries for paths through the renamed attribute."],"tags":["hibernate","any-mapping","hql","property-not-found","query"],"backgroundTag":"property-not-found","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}