{"record":{"id":"708707d305d5a9ac","repo":"hibernate/hibernate-orm","slug":"query-has-no-select-clause-and-joins-but-no-re","errorCode":null,"errorMessage":"Query has no 'select' clause, and joins, but no result type was given (pass an explicit result type to 'createQuery()')","messagePattern":"Query has no 'select' clause, and joins, 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":1312,"sourceCode":"\t\t\t\t// a subquery ... the following is a bit arbitrary\n\t\t\t\tfinal var selectClause = new SqmSelectClause( false, nodeBuilder );\n\t\t\t\tfromClause.visitRoots( sqmRoot -> {\n\t\t\t\t\tselectClause.addSelection( new SqmSelection<>( sqmRoot, sqmRoot.getAlias(), nodeBuilder) );\n\t\t\t\t\tapplyJoinsToInferredSelectClause( sqmRoot, selectClause );\n\t\t\t\t} );\n\t\t\t\treturn selectClause;\n\t\t\t}\n\t\t\telse {\n\t\t\t\t// no result type was specified (and this isn't a subquery)\n\t\t\t\t// if there's a single root entity with no non-fetch joins,\n\t\t\t\t// we may safely assume the query returns that entity\n\t\t\t\tif ( fromClause.getNumberOfRoots() == 1 ) {\n\t\t\t\t\tfinal var sqmRoot = fromClause.getRoots().get(0);\n\t\t\t\t\tif ( sqmRoot.hasImplicitlySelectableJoin() ) {\n\t\t\t\t\t\t// the entity has joins, and doesn't explicitly have\n\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}","sourceCodeStart":1294,"sourceCodeEnd":1330,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/query/hql/internal/SemanticQueryBuilder.java#L1294-L1330","documentation":"Error \"Query has no 'select' clause, and joins, but no result type was given (pass an explicit result type to 'createQuery()')\" thrown in hibernate/hibernate-orm.","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 an explicit result type to createQuery (e.g. Object[].class).","Add a 'select' clause naming the joined roots."],"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"}