{"record":{"id":"2ea6a314c07a0264","repo":"hibernate/hibernate-orm","slug":"query-has-no-select-clause-and-multiple-root-en","errorCode":null,"errorMessage":"Query has no 'select' clause, and multiple root entities, but no result type was given (pass an explicit result type to 'createQuery()')","messagePattern":"Query has no 'select' clause, and multiple root entities, but no result type was given \\(pass an explicit result type to 'createQuery\\(\\)'\\)","errorType":"exception","errorClass":"SemanticException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/query/hql/internal/SemanticQueryBuilder.java","lineNumber":1328,"sourceCode":"\t\t\t\t\t\t// the alias 'this', so the 'select' list cannot be\n\t\t\t\t\t\t// inferred\n\t\t\t\t\t\tthrow new SemanticException( \"Query has no 'select' clause, and joins, but no result type was given\"\n\t\t\t\t\t\t\t\t+ \" (pass an explicit result type to 'createQuery()')\", query );\n\t\t\t\t\t}\n\t\t\t\t\t// exactly one root entity, and no joins - this includes\n\t\t\t\t\t// the case where JPA says the entity has an implicit alias\n\t\t\t\t\t// 'this', and that we should infer 'select this', but we\n\t\t\t\t\t// accept even the case where the entity has an explicit\n\t\t\t\t\t// alias, and infer 'select explicit_alias'\n\t\t\t\t\tfinal var selectClause =\n\t\t\t\t\t\t\tnew SqmSelectClause( false, 1, nodeBuilder );\n\t\t\t\t\tselectClause.addSelection( new SqmSelection<>( sqmRoot, sqmRoot.getAlias(), nodeBuilder) );\n\t\t\t\t\treturn selectClause;\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\t// there's more than one entity, and no entity is 'this',\n\t\t\t\t\t// therefore the 'select' list cannot be inferred\n\t\t\t\t\tthrow new SemanticException( \"Query has no 'select' clause, and multiple root entities, but no result type was given\"\n\t\t\t\t\t\t\t+ \" (pass an explicit result type to 'createQuery()')\", query );\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\t// we have an explicit result type, so we can use that to\n\t\t\t// help infer the 'select' list\n\t\t\tif ( creationContext.getJpaMetamodel().findEntityType( expectedResultType ) != null ) {\n\t\t\t\t// the result type is an entity class\n\t\t\t\tif ( fromClause.getNumberOfRoots() > 1 ) {\n\t\t\t\t\t// multiple root entities\n\t\t\t\t\tthrow new SemanticException( \"Query has no 'select' clause, and multiple root entities, but query result type is an entity class\"\n\t\t\t\t\t\t\t+ \" (specify an explicit 'select' list, or a different result type, for example, 'Object[].class')\",\n\t\t\t\t\t\t\tquery );\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tfinal var sqmRoot = fromClause.getRoots().get(0);\n\t\t\t\t\tif ( sqmRoot instanceof SqmCteRoot ) {","sourceCodeStart":1310,"sourceCodeEnd":1346,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/query/hql/internal/SemanticQueryBuilder.java#L1310-L1346","documentation":"Thrown while inferring an implicit select list: the query has no 'select' clause, its 'from' clause declares multiple root entities, and no result type was given to createQuery(). With multiple roots Hibernate cannot decide what to return, so the caller must pass an explicit result type or write a 'select' clause.","triggerScenarios":"The HQL/JPQL query violates a structural language rule.","commonSituations":"Writing queries with missing select/from, mismatched group by positions, or invalid ordering constructs.","solutions":["Pass Object[].class (or another explicit result type) to createQuery.","Add an explicit 'select' list choosing the needed roots."],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}