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

  1. Pass exactly one numeric expression to avg(): 'select avg(e.salary) from Employee e'
  2. For multi-argument needs, compute (a+b)/2 yourself or use avg over distinct subqueries instead of stuffing arguments into avg()
  3. 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

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


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