hibernate/hibernate-orm · error · CoercionException
Cannot coerce value '%s' [%s] to Float
Error message
Cannot coerce value '%s' [%s] to Float
What it means
FloatJavaType.coerce(Object) is the last-chance conversion of an arbitrary value to Float for float-typed attributes and parameters: it delegates to coerceOrNull and throws CoercionException('Cannot coerce value ... to Float'), naming the value and its class, when no coercion rule matched.
Source
Thrown at hibernate-core/src/main/java/org/hibernate/type/descriptor/java/FloatJavaType.java:173
@Override
public int getDefaultSqlPrecision(Dialect dialect, JdbcType jdbcType) {
return jdbcType.isFloat()
// this is usually the number of *binary* digits
// in a single-precision FP number
? dialect.getFloatPrecision()
// this is the number of decimal digits in a Java float
: 8;
}
@Override
public @Nullable Float 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 Float",
value,
value.getClass().getName()
)
);
}
return coerced;
}
@Override
public @Nullable Float coerceOrNull(@Nonnull Object value) {
if ( value instanceof Float floatValue ) {
return floatValue;
}
if ( value instanceof Number number ) {View on GitHub (pinned to fad1729dce)
Solutions
- Convert to Float yourself before binding: Float.parseFloat on a normalized string or ((Number) v).floatValue()
- Fix the producer of the value (correct Locale, trimmed input)
- Type the query parameter explicitly so coercion is not guessed
- Catch CoercionException at the boundary and map it to a validation error
Example fix
// before
q.setParameter("ratio", userInput); // String "abc" -> CoercionException
// after
q.setParameter("ratio", Float.parseFloat(userInput.trim())); Defensive patterns
Strategy: type-guard
Validate before calling
static Float toFloatOrNull(Object v) {
try {
return v instanceof Number n ? n.floatValue()
: Float.parseFloat(String.valueOf(v).trim());
} catch (RuntimeException e) {
return null;
}
} Type guard
static boolean coercibleToFloat(Object v) {
if (v == null || v instanceof Number || v instanceof Boolean) return true;
return v instanceof String s && s.trim().matches("[+-]?([0-9]+(\\.[0-9]*)?|\\.[0-9]+)([eE][+-]?[0-9]+)?");
} Try / catch
try {
return q.setParameter("ratio", value).getSingleResult();
} catch (CoercionException e) {
throw new BadRequestException("ratio must be numeric", e);
} Prevention
- Convert external input to Float before binding
- Normalize decimal separators and trim whitespace at the edge
- Type parameters explicitly instead of relying on coercion
When it happens
Trigger: Binding a query parameter or setting a float-typed attribute with an unconvertible value: non-numeric or locale-formatted strings, arbitrary object types (Date, POJO, char[]) that the coercion helper does not recognize.
Common situations: setParameter with user input parsed as the wrong type; JSON/deserializer output mapped directly onto float fields; consuming loosely typed maps.
Related errors
- Unable to coerce Double Float `%s` as BigInteger: not a whol
- Cannot coerce Float value `%s` to Double : underflow
- Cannot coerce value '%s' [%s] to Double
- Cannot coerce value '%s' [%s] to Integer
- Cannot coerce Float value `%s` to Byte : not a whole number
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/12eed653ca357453.
Report an issue: GitHub.