{"record":{"id":"f4199243af702ae4","repo":"hibernate/hibernate-orm","slug":"root-entity-of-type-s-did-not-have-the-given-ty","errorCode":null,"errorMessage":"Root entity of type '%s' did not have the given type '%s'","messagePattern":"Root entity of type '(.+?)' did not have the given type '(.+?)'","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/query/sqm/tree/spi/select/AbstractSqmSelectQuery.java","lineNumber":299,"sourceCode":"\t */\n\t@Nonnull\n\tpublic <E> JpaRoot<? extends E> getRoot(String alias, Class<E> type) {\n\t\tfor ( var root : getQuerySpec().getRootList() ) {\n\t\t\tfinal String rootAlias = root.getAlias();\n\t\t\tif ( rootAlias != null && rootAlias.equals( alias ) ) {\n\t\t\t\treturn castRoot( root, type );\n\t\t\t}\n\t\t}\n\t\tthrow new IllegalArgumentException( \"No root entity with alias \" + alias );\n\t}\n\n\tprivate static <E> JpaRoot<? extends E> castRoot(JpaRoot<?> root, Class<E> type) {\n\t\tfinal var rootEntityType = root.getJavaType();\n\t\tif ( rootEntityType == null ) {\n\t\t\tthrow new AssertionFailure( \"Java type of root entity was null\" );\n\t\t}\n\t\tif ( !type.isAssignableFrom( rootEntityType ) ) {\n\t\t\tthrow new IllegalArgumentException( \"Root entity of type '\" + rootEntityType.getTypeName()\n\t\t\t\t\t\t\t\t\t\t\t\t+ \"' did not have the given type '\" + type.getTypeName() + \"'\");\n\t\t}\n\t\t@SuppressWarnings(\"unchecked\") // safe, we just checked\n\t\tfinal var result = (JpaRoot<? extends E>) root;\n\t\treturn result;\n\t}\n\n\t@Nonnull\n\t@Override\n\tpublic <X> SqmRoot<X> from(@Nonnull Class<X> entityClass) {\n\t\treturn addRoot(\n\t\t\t\tnew SqmRoot<>(\n\t\t\t\t\t\tnodeBuilder().getDomainModel().entity( entityClass ),\n\t\t\t\t\t\tgenerateAlias(),\n\t\t\t\t\t\ttrue,\n\t\t\t\t\t\tnodeBuilder()\n\t\t\t\t)\n\t\t);","sourceCodeStart":281,"sourceCodeEnd":317,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/spi/select/AbstractSqmSelectQuery.java#L281-L317","documentation":"Error \"Root entity of type '%s' did not have the given type '%s'\" thrown in hibernate/hibernate-orm.","triggerScenarios":"A root entity was resolved by alias but its entity type does not match the type requested by the caller (treat/narrow by type).","commonSituations":"Requesting a root of an unrelated subclass, or confusing two roots with similar aliases in a multi-root criteria query.","solutions":["Verify the root entity's type before calling the typed access: the root must be assignable to the requested type.","Pass the correct expected type (the entity class used when the root was created, or a subclass of it).","If the alias refers to a different root than intended, list the roots and pick the one with the matching entity type."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}