hibernate/hibernate-orm · error · SemanticException

Can't use expression

Error message

Can't use expression '{} without explicit name in xmlforest function, because XML element names can only be derived from path expressions.

What it means

Thrown by SemanticQueryBuilder.visitXmlforestFunction when an argument of xmlforest() has no explicit element name and the argument expression is not a path expression. XML element names in xmlforest can only be derived from attribute/alias names of path expressions, so a bare computed expression without a name is rejected.

Solutions

  1. Give the expression an explicit name (AS name) in the xmlforest function.
  2. Use a path expression so the element name can be derived.
Defensive patterns

Strategy: validation

When it happens

Trigger: A runtime precondition of the Hibernate API is violated.

Common situations: Occurs when application code or configuration does not satisfy the documented contract of the API being called.


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

Appendix: source

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

		xmlelement.content( visitExpressions( ctx ) );
		return xmlelement;
	}

	@Override
	public SqmExpression<?> visitXmlforestFunction(HqlParser.XmlforestFunctionContext ctx) {
		final ArrayList<SqmExpression<?>> elementExpressions = new ArrayList<>( ctx.getChildCount() >> 1 );
		for ( int i = 2; i < ctx.getChildCount(); i++ ) {
			if ( ctx.getChild( i ) instanceof HqlParser.ExpressionOrPredicateContext exprCtx ) {
				final var expression = (SqmExpression<?>) exprCtx.accept( this );
				if ( i + 2 < ctx.getChildCount() && ctx.getChild( i + 2 ) instanceof HqlParser.IdentifierContext identifierContext ) {
					final String name = visitIdentifier( identifierContext );
					elementExpressions.add( new SqmNamedExpression<>( expression, name ) );
					i += 2;
				}
				else {
					if ( !( expression instanceof SqmPath<?> path )
							|| !( path.getModel() instanceof PersistentAttribute<?, ?> attribute ) ) {
						throw new SemanticException(
								"Can't use expression '" + exprCtx.getText() + " without explicit name in xmlforest function" +
										", because XML element names can only be derived from path expressions.",
								query
						);
					}
					elementExpressions.add( new SqmNamedExpression<>( expression, attribute.getName() ) );
				}
			}
		}
		return nodeBuilder().xmlforest( elementExpressions );
	}

	@Override
	public SqmExpression<?> visitXmlpiFunction(HqlParser.XmlpiFunctionContext ctx) {
		final String name = visitIdentifier( ctx.identifier() );
		final var exprCtx = ctx.expression();
		//noinspection unchecked
		return exprCtx == null

View on GitHub (pinned to fad1729dce)