hibernate/hibernate-orm · error · FunctionArgumentException

Invalid XML element name passed to 'xmlforest()': %s

Error message

Invalid XML element name passed to 'xmlforest()': %s

What it means

Each argument of 'xmlforest()' becomes an XML element whose tag name is taken from the argument alias, so the alias itself must be a legal XML name. XmlForestFunction calls XmlHelper.isValidXmlName on every alias and throws FunctionArgumentException naming the offending value when the check fails. A valid name is non-empty, starts with a letter, '_' or ':', does not start with 'xml' (case-insensitive), and contains only letters, digits, '_', ':', '-', '.'.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/dialect/function/xml/XmlForestFunction.java:54

				StandardArgumentsValidators.composite(
						StandardArgumentsValidators.min( 1 ),
						new ArgumentsValidator() {
							@Override
							public void validate(
									List<? extends SqmTypedNode<?>> arguments,
									String functionName,
									BindingContext bindingContext) {
								for ( int i = 0; i < arguments.size(); i++ ) {
									if ( !( arguments.get( i ) instanceof SqmNamedExpression<?> namedExpression ) ) {
										throw new FunctionArgumentException(
												String.format(
														"Parameter %d of function 'xmlforest()' is not named",
														i
												)
										);
									}
									if ( !XmlHelper.isValidXmlName( namedExpression.getName() ) ) {
										throw new FunctionArgumentException(
												String.format(
														"Invalid XML element name passed to 'xmlforest()': %s",
														namedExpression.getName()
												)
										);
									}
								}
							}

						}
				),
				StandardFunctionReturnTypeResolvers.invariant(
						typeConfiguration.getBasicTypeRegistry().resolve( String.class, SqlTypes.SQLXML )
				),
				null
		);
	}

View on GitHub (pinned to fad1729dce)

Solutions

  1. Rewrite the offending alias to start with a letter or underscore and use only [A-Za-z0-9_:. -], e.g. as "first_name" instead of as "first name"
  2. Rename aliases beginning with 'xml'/'XML' (reserved prefix) such as "xmlId" to "dataId"
  3. Validate generated aliases with the same rules before assembling the HQL string
  4. Move XML construction to Java code if the required tag names cannot be made legal

Example fix

// before
select xmlforest(p.name as "first name", p.id as "xmlId") from Person p

// after
select xmlforest(p.name as "first_name", p.id as "dataId") from Person p
Defensive patterns

Strategy: validation

Validate before calling

static boolean isValidXmlName(String name) {
    if (name == null || name.isEmpty()
            || !(Character.isLetter(name.charAt(0)) || name.charAt(0) == '_' || name.charAt(0) == ':')
            || name.regionMatches(true, 0, "xml", 0, 3)) {
        return false;
    }
    for (int i = 1; i < name.length(); i++) {
        char c = name.charAt(i);
        if (!(Character.isLetterOrDigit(c) || c == '_' || c == ':' || c == '-' || c == '.')) {
            return false;
        }
    }
    return true;
}

if (aliases.stream().anyMatch(a -> !isValidXmlName(a))) throw new IllegalArgumentException("invalid xmlforest alias");

Try / catch

try {
    return session.createQuery(hql, String.class).getSingleResult();
}
catch (FunctionArgumentException e) {
    // message names the invalid element name - map it to the offending alias in your query builder
    throw new IllegalArgumentException("xmlforest() alias is not a valid XML element name: " + e.getMessage(), e);
}

Prevention

When it happens

Trigger: An aliased xmlforest argument whose alias is not a valid XML name: select xmlforest(p.name as "xmlName") (reserved xml prefix), as "first name" (space), as "2ndPhone" (starts with digit), or as "id@db" (illegal character '@'). The exception is raised during SQM validation, before SQL execution.

Common situations: Reusing database column labels or JSON property names as xmlforest aliases; mapping domain fields like 'xmlData' or '24hFlag' straight into generated HQL; migrating from xmlquery/xmltable code where such names were only string data, not tag names.

Related errors


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