hibernate/hibernate-orm · error · CoercionException

Cannot coerce value '%s' [%s] to Integer

Error message

Cannot coerce value '%s' [%s] to Integer

What it means

IntegerJavaType.coerce(Object) is the last-chance conversion of an arbitrary value to Integer for int-typed attributes and parameters: it delegates to coerceOrNull and throws CoercionException('Cannot coerce value ... to Integer'), naming the value and its class, when no coercion rule applied.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/type/descriptor/java/IntegerJavaType.java:163

	@Override
	public int getDefaultSqlPrecision(Dialect dialect, JdbcType jdbcType) {
		return 10;
	}

	@Override
	public int getDefaultSqlScale(Dialect dialect, JdbcType jdbcType) {
		return 0;
	}

	@Override
	public @Nullable Integer coerce(@Nullable Object value) {
		if ( value == null ) {
			return null;
		}
		final var coerced = coerceOrNull( value );
		if ( coerced == null ) {
			throw new CoercionException(
					String.format(
							Locale.ROOT,
							"Cannot coerce value '%s' [%s] to Integer",
							value,
							value.getClass().getName()
					)
			);
		}
		return coerced;
	}

	@Override
	public @Nullable Integer coerceOrNull(@Nonnull Object value) {
		if ( value instanceof Integer integer ) {
			return integer;
		}

		if ( value instanceof Short shortValue ) {

View on GitHub (pinned to fad1729dce)

Solutions

  1. Convert to Integer yourself before binding: Integer.parseInt on a validated string or ((Number) v).intValue()
  2. Fix the producer: parse and trim input, reject fractional strings for int fields
  3. Type the query parameter explicitly with setParameter(name, value, type)
  4. Catch CoercionException at the boundary and return a validation error

Example fix

// before
q.setParameter("limit", request.getParameter("limit")); // "20items" -> CoercionException

// after
q.setParameter("limit", Integer.parseInt(request.getParameter("limit").trim()));
Defensive patterns

Strategy: type-guard

Validate before calling

static Integer toIntegerOrNull(Object v) {
    try {
        return v instanceof Number n ? n.intValue()
                : Integer.parseInt(String.valueOf(v).trim());
    } catch (RuntimeException e) {
        return null;
    }
}

Type guard

static boolean coercibleToInteger(Object v) {
    if (v == null || v instanceof Number || v instanceof Boolean) return true;
    return v instanceof String s && s.trim().matches("[+-]?[0-9]+");
}

Try / catch

try {
    return q.setParameter("limit", value).getSingleResult();
} catch (CoercionException e) {
    throw new BadRequestException("limit must be an integer", e);
}

Prevention

When it happens

Trigger: Binding a parameter or setting an int-typed attribute with an unconvertible value: strings like 'abc', '' or '12.5' that do not parse as integers, or arbitrary object types (Date, POJO) the coercion helper does not know.

Common situations: Web/JSON input passed unparsed to setParameter; enum or boolean values reaching an int field via a bad mapping; locale-formatted numeric strings.

Related errors


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