{"record":{"id":"12eed653ca357453","repo":"hibernate/hibernate-orm","slug":"cannot-coerce-value-s-s-to-float","errorCode":null,"errorMessage":"Cannot coerce value '%s' [%s] to Float","messagePattern":"Cannot coerce value '(.+?)' \\[(.+?)\\] to Float","errorType":"exception","errorClass":"CoercionException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/type/descriptor/java/FloatJavaType.java","lineNumber":173,"sourceCode":"\n\t@Override\n\tpublic int getDefaultSqlPrecision(Dialect dialect, JdbcType jdbcType) {\n\t\treturn jdbcType.isFloat()\n\t\t\t\t// this is usually the number of *binary* digits\n\t\t\t\t// in a single-precision FP number\n\t\t\t\t? dialect.getFloatPrecision()\n\t\t\t\t// this is the number of decimal digits in a Java float\n\t\t\t\t: 8;\n\t}\n\n\t@Override\n\tpublic @Nullable Float coerce(@Nullable Object value) {\n\t\tif ( value == null ) {\n\t\t\treturn null;\n\t\t}\n\t\tfinal var coerced = coerceOrNull( value );\n\t\tif ( coerced == null ) {\n\t\t\tthrow new CoercionException(\n\t\t\t\t\tString.format(\n\t\t\t\t\t\t\tLocale.ROOT,\n\t\t\t\t\t\t\t\"Cannot coerce value '%s' [%s] to Float\",\n\t\t\t\t\t\t\tvalue,\n\t\t\t\t\t\t\tvalue.getClass().getName()\n\t\t\t\t\t)\n\t\t\t);\n\t\t}\n\t\treturn coerced;\n\t}\n\n\t@Override\n\tpublic @Nullable Float coerceOrNull(@Nonnull Object value) {\n\t\tif ( value instanceof Float floatValue ) {\n\t\t\treturn floatValue;\n\t\t}\n\n\t\tif ( value instanceof Number number ) {","sourceCodeStart":155,"sourceCodeEnd":191,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/FloatJavaType.java#L155-L191","documentation":"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.","triggerScenarios":"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.","commonSituations":"setParameter with user input parsed as the wrong type; JSON/deserializer output mapped directly onto float fields; consuming loosely typed maps.","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"],"exampleFix":"// before\nq.setParameter(\"ratio\", userInput); // String \"abc\" -> CoercionException\n\n// after\nq.setParameter(\"ratio\", Float.parseFloat(userInput.trim()));","handlingStrategy":"type-guard","validationCode":"static Float toFloatOrNull(Object v) {\n    try {\n        return v instanceof Number n ? n.floatValue()\n                : Float.parseFloat(String.valueOf(v).trim());\n    } catch (RuntimeException e) {\n        return null;\n    }\n}","typeGuard":"static boolean coercibleToFloat(Object v) {\n    if (v == null || v instanceof Number || v instanceof Boolean) return true;\n    return v instanceof String s && s.trim().matches(\"[+-]?([0-9]+(\\\\.[0-9]*)?|\\\\.[0-9]+)([eE][+-]?[0-9]+)?\");\n}","tryCatchPattern":"try {\n    return q.setParameter(\"ratio\", value).getSingleResult();\n} catch (CoercionException e) {\n    throw new BadRequestException(\"ratio must be numeric\", e);\n}","preventionTips":["Convert external input to Float before binding","Normalize decimal separators and trim whitespace at the edge","Type parameters explicitly instead of relying on coercion"],"tags":["hibernate","type-coercion","numeric","float","query-parameter"],"backgroundTag":"numeric-type-coercion-failed","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}