hibernate/hibernate-orm · error · FunctionArgumentException
Invalid XML attribute name passed to 'xmlattributes()': %s
Error message
Invalid XML attribute name passed to 'xmlattributes()': %s
What it means
Thrown when Hibernate validates the arguments of the HQL 'xmlelement()' function and an attribute name supplied through 'xmlattributes()' fails XmlHelper.isValidXmlName(). A valid XML name must be non-empty, start with a letter, '_' or ':', must not start with 'xml' (case-insensitive, reserved per the XML spec), and may only contain letters, digits, '_', ':', '-', or '.'. The check runs while the SQM query is being parsed/validated, before any SQL is executed.
Source
Thrown at hibernate-core/src/main/java/org/hibernate/dialect/function/xml/XmlElementFunction.java:70
List<? extends SqmTypedNode<?>> arguments,
String functionName,
BindingContext bindingContext) {
//noinspection unchecked
final var literal = (SqmLiteral<String>) arguments.get( 0 );
final String elementName = literal.getLiteralValue();
if ( !XmlHelper.isValidXmlName( elementName ) ) {
throw new FunctionArgumentException(
String.format(
"Invalid XML element name passed to 'xmlelement()': %s",
elementName
)
);
}
if ( arguments.size() > 1
&& arguments.get( 1 ) instanceof SqmXmlAttributesExpression attributesExpression ) {
for ( var entry : attributesExpression.getAttributes().entrySet() ) {
if ( !XmlHelper.isValidXmlName( entry.getKey() ) ) {
throw new FunctionArgumentException(
String.format(
"Invalid XML attribute name passed to 'xmlattributes()': %s",
entry.getKey()
)
);
}
}
}
}
}
),
StandardFunctionReturnTypeResolvers.invariant(
typeConfiguration.getBasicTypeRegistry().resolve( String.class, SqlTypes.SQLXML )
),
null
);
}
View on GitHub (pinned to fad1729dce)
Solutions
- Change the alias used inside xmlattributes() so it starts with a letter or underscore, e.g. xmlattributes(p.name as "name") instead of as "1stName"
- Remove spaces and special characters from the alias (use '-' or '_' instead of ' ', no '@'/'#')
- Rename aliases that start with 'xml'/'XML' (case-insensitive) to something else, e.g. "dataId" instead of "xmlId"
- If a non-legal name is mandatory, build the XML on the Java side instead of via xmlelement()
Example fix
// before select xmlelement(name "user", xmlattributes(p.name as "1st_name", p.id as "xmlId")) from Person p // after select xmlelement(name "user", xmlattributes(p.name as "first_name", p.id as "dataId")) from Person p
Defensive patterns
Strategy: validation
Validate before calling
// Mirror of XmlHelper.isValidXmlName - run before assembling HQL
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;
}
// before building the query:
if (!isValidXmlName(alias)) throw new IllegalArgumentException("alias '" + alias + "' is not a valid XML attribute name"); Try / catch
try {
return session.createQuery(hql, String.class).getSingleResult();
}
catch (FunctionArgumentException e) { // org.hibernate.query.sqm.produce.function
throw new IllegalArgumentException("xmlattributes() alias is not a valid XML name: " + e.getMessage(), e);
} Prevention
- Alias every xmlattributes() argument and keep aliases to [A-Za-z_][A-Za-z0-9_:. -]*
- Never start an alias with 'xml' (case-insensitive, reserved)
- For dynamic query builders, validate aliases with the XmlHelper name rules before concatenating HQL
- Keep a test that executes every generated HQL report query so invalid names fail in CI, not production
When it happens
Trigger: An HQL/criteria query calls xmlelement(name "person", xmlattributes(p.name as "1stName")) or uses an alias that starts with 'xml' (e.g. as "xmlId"), contains a space (as "first name"), or contains characters like '@', '#', or '='. Any of these makes isValidXmlName return false for the attribute map key and FunctionArgumentException is thrown at query creation.
Common situations: Porting SQL/XML queries from native SQL to HQL where quoted aliases with spaces or numeric prefixes were legal; generating HQL dynamically from user-supplied column labels; XML feeds that want attribute names starting with digits ('2fa', '24h') or with the reserved 'xml' prefix.
Related errors
- Parameter %d of function 'xmlforest()' is not named
- Invalid XML element name passed to 'xmlforest()': %s
- Function %s() has %d parameters, but %d arguments given
- Insert conflict 'do update' clause with constraint name is n
- field type not supported on Derby: " + unit
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/89cc603620af4519.
Report an issue: GitHub.