hibernate/hibernate-orm · error · FunctionArgumentException
Function %s() has %d parameters, but %d arguments given
Error message
Function %s() has %d parameters, but %d arguments given
What it means
AvgFunction's built-in ArgumentsValidator rejects any avg() invocation whose argument count is not exactly 1. HQL/JPQL avg() is defined over a single numeric expression, so avg() with zero or 2+ arguments fails during query compilation with a FunctionArgumentException before any SQL is generated.
Source
Thrown at hibernate-core/src/main/java/org/hibernate/dialect/function/AvgFunction.java:160
}
}
@Override
public String getArgumentListSignature() {
return "(NUMERIC arg)";
}
public static class Validator implements ArgumentsValidator {
public static final ArgumentsValidator INSTANCE = new Validator();
@Override
public void validate(
List<? extends SqmTypedNode<?>> arguments,
String functionName,
BindingContext bindingContext) {
if ( arguments.size() != 1 ) {
throw new FunctionArgumentException(
String.format(
Locale.ROOT,
"Function %s() has %d parameters, but %d arguments given",
functionName,
1,
arguments.size()
)
);
}
final var expressible = arguments.get( 0 ).getExpressible();
if ( expressible != null ) {
final var domainType = expressible.getSqmType();
if ( domainType != null ) {
final var jdbcType = getJdbcType( domainType, bindingContext.getTypeConfiguration() );
if ( !isNumeric( jdbcType ) ) {
throw new FunctionArgumentException(
String.format(
"Parameter %d of function '%s()' has type '%s', but argument is of type '%s'",View on GitHub (pinned to fad1729dce)
Solutions
- Pass exactly one numeric expression to avg(): 'select avg(e.salary) from Employee e'
- For multi-argument needs, compute (a+b)/2 yourself or use avg over distinct subqueries instead of stuffing arguments into avg()
- If you translated from native SQL with OVER clauses, drop the extra args and rely on HQL aggregate semantics or native queries
Example fix
// before
List<Double> r = session.createQuery("select avg(e.salary, e.bonus) from Employee e", Double.class).list();
// after
List<Double> r = session.createQuery("select avg(e.salary + e.bonus) from Employee e", Double.class).list(); Defensive patterns
Strategy: validation
Validate before calling
// wrap dynamic HQL construction
String avgArg = singleNumericExpression; // exactly one expression, validated upstream
String hql = "select avg(" + avgArg + ") from " + entity;
assert avgArg.split(",").length == 1; Try / catch
catch (FunctionArgumentException e) {
// rewrite or reject the query; arity errors are deterministic, never retry
throw new IllegalArgumentException("Bad aggregate in generated query: " + hql, e);
} Prevention
- For generated HQL, build aggregates through helper methods that take exactly one expression parameter
- Lint JPQL strings for avg(...,...) in code review
- Test query-building code paths with unit tests before hitting the DB
When it happens
Trigger: Compiling an HQL/JPQL/criteria query containing avg() with the wrong arity, e.g. 'select avg(e.salary, e.bonus) from Employee e' or 'select avg() ...'; also happens when a CriteriaBuilder avg() call is built with the wrong parameter by accident.
Common situations: Typos or copy-paste in JPQL aggregates; frameworks that build dynamic HQL and concatenate an expression list into avg(); migrating raw SQL like AVG(a) OVER (PARTITION BY b) to HQL and keeping extra arguments.
Related errors
- Parameter %d of function '%s()' has type '%s', but argument
- Invalid XML attribute name passed to 'xmlattributes()': %s
- Parameter %d of function 'xmlforest()' is not named
- Invalid XML element name passed to 'xmlforest()': %s
- Ordinal parameter labels start from '?%s' (ordinal parameter
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/c587f358be0f701e.
Report an issue: GitHub.