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
- Convert to Integer yourself before binding: Integer.parseInt on a validated string or ((Number) v).intValue()
- Fix the producer: parse and trim input, reject fractional strings for int fields
- Type the query parameter explicitly with setParameter(name, value, type)
- 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
- Parse and validate integer input at the request boundary
- Reject fractional strings for int fields early
- Pass explicit parameter types on queries
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
- Cannot coerce value '%s' [%s] to Double
- Cannot coerce value '%s' [%s] to Float
- Unable to coerce Double value `%s` to Integer: not a whole n
- Unable to coerce Float value `%s` to Integer: not a whole nu
- Unable to coerce Double Float `%s` as BigInteger: not a whol
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/885c64ef5fc8dfc6.
Report an issue: GitHub.