{"record":{"id":"a54a28e43f62293c","repo":"hibernate/hibernate-orm","slug":"cannot-dereference-an-entity-name","errorCode":null,"errorMessage":"Cannot dereference an entity name","messagePattern":"Cannot dereference an entity name","errorType":"exception","errorClass":"HqlInterpretationException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/query/sqm/tree/spi/expression/SqmAnyDiscriminatorValue.java","lineNumber":86,"sourceCode":"\tpublic EntityDomainType getEntityValue() {\n\t\treturn value;\n\t}\n\n\tpublic String getPathName() {\n\t\treturn pathName;\n\t}\n\n\t@Override\n\tpublic String asLoggableText() {\n\t\treturn getEntityValue().getName();\n\t}\n\n\t@Override\n\tpublic SemanticPathPart resolvePathPart(\n\t\t\tString name,\n\t\t\tboolean isTerminal,\n\t\t\tSqmCreationState creationState) {\n\t\tthrow new HqlInterpretationException( \"Cannot dereference an entity name\" );\n\t}\n\n\t@Override\n\tpublic SqmPath<?> resolveIndexedAccess(\n\t\t\tSqmExpression<?> selector,\n\t\t\tboolean isTerminal,\n\t\t\tSqmCreationState creationState) {\n\t\tthrow new HqlInterpretationException( \"Cannot dereference an entity name\" );\n\t}\n\n\t@Override\n\tpublic void appendHqlString(StringBuilder hql, SqmRenderContext context) {\n\t\thql.append( getEntityValue().getName() );\n\t}\n\n\t@Override\n\tpublic boolean equals(@Nullable Object object) {\n\t\treturn object instanceof SqmAnyDiscriminatorValue<?> that","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/spi/expression/SqmAnyDiscriminatorValue.java#L68-L104","documentation":"SqmAnyDiscriminatorValue is the SQM node produced by HQL TYPE(e) — it represents an entity name token from the discriminator, not a path. Because an entity name has no members, resolvePathPart (attribute navigation like 'type(p).name') throws HqlInterpretationException('Cannot dereference an entity name') at query interpretation time.","triggerScenarios":"HQL that dots into a TYPE() expression: 'select type(p).name from Payment p', 'where type(p).id = 1', or any path continuation after type(...); criteria code that calls .get(...) on the expression returned by cb.type(path).","commonSituations":"Developers expecting TYPE(e) to return a string-like value they can select parts of; dynamic query builders that concatenate '.x' onto arbitrary expressions; migrating queries that used discriminators as strings in Hibernate 5 HQL.","solutions":["Use the type token whole: 'select type(p) from Payment p' or compare it directly: 'where type(p) = CreditCardPayment'.","To get the name as a string, materialize the entity and use getClass().getName()/getEntityName() in Java, or select the discriminator column via native/typed projection if the mapping exposes it.","For per-subtype logic use TREAT AS or separate queries per concrete type.","Fix dynamic query builders to never append attribute paths to non-path expressions like TYPE(...)."],"exampleFix":"-- before (HQL)\nselect type(p).name from Payment p  -- cannot dereference an entity name\n-- after\nselect type(p) from Payment p  -- use the token itself\n-- or: select p from Payment p where type(p) = CreditCardPayment","handlingStrategy":"try-catch","validationCode":"// Lint dynamically built HQL: TYPE(...) must not be followed by '.' or '['\nstatic boolean hqlDereferencesType(String hql) {\n    return hql.matches(\"(?is).*type\\\\s*\\\\([^)]*\\\\)\\\\s*(\\\\.|\\\\[).*\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    return em.createQuery(hql, resultClass);\n} catch (HqlInterpretationException badPath) {\n    throw new IllegalArgumentException(\"Query dereferences an entity name (TYPE(...).attr): \" + hql, badPath);\n}","preventionTips":["Treat TYPE(e) as an opaque token: compare it, never navigate off it.","Add unit tests that createQuery() every HQL string at build time to fail fast.","In string-built HQL, never append attribute fragments to type() expressions."],"tags":["hibernate","hql","discriminator","type-expression","path-resolution","query-interpretation"],"backgroundTag":"illegal-path-dereference","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}