hibernate/hibernate-orm · error · SemanticException

Query has no 'select' clause, and the 'from' clause refers t

Error message

Query has no 'select' clause, and the 'from' clause refers to a CTE, but query result type is an entity class (specify an explicit 'select' list)

What it means

Error "Query has no 'select' clause, and the 'from' clause refers to a CTE, but query result type is an entity class (specify an explicit 'select' list)" thrown in hibernate/hibernate-orm.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/query/hql/internal/SemanticQueryBuilder.java:1347

							+ " (pass an explicit result type to 'createQuery()')", query );
				}
			}
		}
		else {
			// we have an explicit result type, so we can use that to
			// help infer the 'select' list
			if ( creationContext.getJpaMetamodel().findEntityType( expectedResultType ) != null ) {
				// the result type is an entity class
				if ( fromClause.getNumberOfRoots() > 1 ) {
					// multiple root entities
					throw new SemanticException( "Query has no 'select' clause, and multiple root entities, but query result type is an entity class"
							+ " (specify an explicit 'select' list, or a different result type, for example, 'Object[].class')",
							query );
				}
				else {
					final var sqmRoot = fromClause.getRoots().get(0);
					if ( sqmRoot instanceof SqmCteRoot ) {
						throw new SemanticException( "Query has no 'select' clause, and the 'from' clause refers to a CTE, but query result type is an entity class"
								+ " (specify an explicit 'select' list)",
								query );
					}
					else {
						// exactly one root entity, return it
						// (joined entities are not returned)
						final var selectClause =
								new SqmSelectClause( false, 1, nodeBuilder );
						selectClause.addSelection( new SqmSelection<>( sqmRoot, sqmRoot.getAlias(), nodeBuilder) );
						return selectClause;
					}
				}
			}
			else {
				// the result type is not an entity class, and so
				// it must be some sort of object which packages
				// a multi-element projection list - let's return
				// all root entities and non-fetch joins, and see

View on GitHub (pinned to fad1729dce)

Solutions

  1. Add an explicit 'select' list for the CTE-based query.
  2. Use a non-entity result type matching the CTE columns.

When it happens

Trigger: The HQL/JPQL query violates a structural language rule.

Common situations: Writing queries with missing select/from, mismatched group by positions, or invalid ordering constructs.


AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22). Data as JSON: /api/errors/8f9158064a6bccbd. Report an issue: GitHub.