{"record":{"id":"2445b72cdce10e38","repo":"hibernate/hibernate-orm","slug":"parameter-d-of-function-xmlforest-is-not-name","errorCode":null,"errorMessage":"Parameter %d of function 'xmlforest()' is not named","messagePattern":"Parameter (.+?) of function 'xmlforest\\(\\)' is not named","errorType":"exception","errorClass":"FunctionArgumentException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/dialect/function/xml/XmlForestFunction.java","lineNumber":46,"sourceCode":" * Standard xmlforest function.\n */\npublic class XmlForestFunction extends AbstractSqmSelfRenderingFunctionDescriptor {\n\n\tpublic XmlForestFunction(TypeConfiguration typeConfiguration) {\n\t\tsuper(\n\t\t\t\t\"xmlforest\",\n\t\t\t\tFunctionKind.NORMAL,\n\t\t\t\tStandardArgumentsValidators.composite(\n\t\t\t\t\t\tStandardArgumentsValidators.min( 1 ),\n\t\t\t\t\t\tnew ArgumentsValidator() {\n\t\t\t\t\t\t\t@Override\n\t\t\t\t\t\t\tpublic void validate(\n\t\t\t\t\t\t\t\t\tList<? extends SqmTypedNode<?>> arguments,\n\t\t\t\t\t\t\t\t\tString functionName,\n\t\t\t\t\t\t\t\t\tBindingContext bindingContext) {\n\t\t\t\t\t\t\t\tfor ( int i = 0; i < arguments.size(); i++ ) {\n\t\t\t\t\t\t\t\t\tif ( !( arguments.get( i ) instanceof SqmNamedExpression<?> namedExpression ) ) {\n\t\t\t\t\t\t\t\t\t\tthrow new FunctionArgumentException(\n\t\t\t\t\t\t\t\t\t\t\t\tString.format(\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\"Parameter %d of function 'xmlforest()' is not named\",\n\t\t\t\t\t\t\t\t\t\t\t\t\t\ti\n\t\t\t\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\tif ( !XmlHelper.isValidXmlName( namedExpression.getName() ) ) {\n\t\t\t\t\t\t\t\t\t\tthrow new FunctionArgumentException(\n\t\t\t\t\t\t\t\t\t\t\t\tString.format(\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\"Invalid XML element name passed to 'xmlforest()': %s\",\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tnamedExpression.getName()\n\t\t\t\t\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t}","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/dialect/function/xml/XmlForestFunction.java#L28-L64","documentation":"The 'xmlforest()' HQL function must emit one XML element per argument, and each element needs a tag name, so every argument has to be an aliased (named) expression (SqmNamedExpression). During SQM validation XmlForestFunction iterates the arguments and throws FunctionArgumentException with the 0-based parameter position when argument i carries no alias. This fails at query translation time, before SQL generation.","triggerScenarios":"Calling xmlforest() with plain, unaliased expressions: select xmlforest(p.name, p.age) from Person p - argument 0 ('p.name') is not named, so 'Parameter 0 of function 'xmlforest()' is not named' is thrown. Also happens when a dynamic query builder omits the 'as alias' clause for one of several arguments.","commonSituations":"Copying native SQL/XML 'xmlforest()' calls into HQL (SQL dialects can derive names from column labels, Hibernate cannot); refactoring a select list and dropping aliases; criteria/HQL generated from reflection over field names without aliasing.","solutions":["Alias every argument of xmlforest(): select xmlforest(p.name as \"name\", p.age as \"age\") from Person p","If you build HQL dynamically, assert each contributed fragment inside xmlforest(...) contains ' as <alias>'","If you do not need per-element names, use a different construction (e.g. xmlelement or client-side mapping) since xmlforest fundamentally requires names"],"exampleFix":"// before\nselect xmlforest(p.name, p.age) from Person p\n\n// after\nselect xmlforest(p.name as \"name\", p.age as \"age\") from Person p","handlingStrategy":"validation","validationCode":"// Before building xmlforest(...) fragments, ensure every argument is aliased\nboolean allAliased = args.stream().allMatch(a -> a.contains(\" as \"));\nif (!allAliased) {\n    throw new IllegalArgumentException(\"every xmlforest() argument needs an alias, e.g. p.name as \\\"name\\\"\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    return session.createQuery(hql, String.class).list();\n}\ncatch (FunctionArgumentException e) {\n    // e.getMessage() contains the 0-based parameter index\n    throw new IllegalArgumentException(\"xmlforest() argument not aliased: \" + e.getMessage(), e);\n}","preventionTips":["Treat xmlforest() like a projection: every expression gets an alias that becomes the XML tag name","Lint generated HQL in CI with a parser or a regex check for unaliased arguments inside xmlforest(...)","Keep xmlforest calls in one query-building module so aliasing cannot be forgotten at call sites"],"tags":["hibernate","hql","xml","query-validation","xmlforest"],"backgroundTag":"missing-query-alias","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}