{"record":{"id":"c7f92873d66233b4","repo":"hibernate/hibernate-orm","slug":"cannot-coerce-value-s-s-to-double","errorCode":null,"errorMessage":"Cannot coerce value '%s' [%s] to Double","messagePattern":"Cannot coerce value '(.+?)' \\[(.+?)\\] to Double","errorType":"exception","errorClass":"CoercionException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/type/descriptor/java/DoubleJavaType.java","lineNumber":175,"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 double-precision FP number\n\t\t\t\t? dialect.getDoublePrecision()\n\t\t\t\t// this is the number of decimal digits in a Java double\n\t\t\t\t: 17;\n\t}\n\n\t@Override\n\tpublic @Nullable Double 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 Double\",\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 Double coerceOrNull(@Nonnull Object value) {\n\t\tif ( value instanceof Double doubleValue ) {\n\t\t\treturn doubleValue;\n\t\t}\n\n\t\tif ( value instanceof Float floatValue ) {","sourceCodeStart":157,"sourceCodeEnd":193,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/DoubleJavaType.java#L157-L193","documentation":"DoubleJavaType.coerce(Object) is Hibernate's last-chance conversion of an arbitrary value to Double for double-typed attributes and query parameters: it delegates to coerceOrNull and throws CoercionException('Cannot coerce value ... to Double'), naming the offending value and its class, when no coercion rule matched and coerceOrNull returned null.","triggerScenarios":"Binding a query parameter or setting a double-typed attribute with a value Hibernate cannot convert: a non-numeric or locale-formatted String ('abc', '', '1,5'), or an arbitrary object type (java.util.Date, a POJO, char[]) that the numeric coercion helper does not know.","commonSituations":"HQL/native setParameter with user input parsed as the wrong type; JSON deserialization straight onto entities; consuming Map<String,Object> rows; locale-formatted numbers from European formats.","solutions":["Convert the value to Double yourself before binding: Double.parseDouble on a normalized string, or ((Number) v).doubleValue()","Fix the producer: parse with the correct Locale, trim blanks, normalize decimal separators","Type the parameter explicitly with setParameter(name, value, type) so coercion is not guessed","Catch CoercionException at the API boundary and return a validation error"],"exampleFix":"// before\nq.setParameter(\"rate\", map.get(\"rate\")); // value is String \"3,5\" -> CoercionException\n\n// after\nq.setParameter(\"rate\", Double.parseDouble(map.get(\"rate\").toString().replace(',', '.')));","handlingStrategy":"type-guard","validationCode":"static Double toDoubleOrNull(Object v) {\n    try {\n        return v instanceof Number n ? n.doubleValue()\n                : Double.parseDouble(String.valueOf(v).trim());\n    } catch (RuntimeException e) {\n        return null;\n    }\n}","typeGuard":"static boolean coercibleToDouble(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(\"rate\", value).getSingleResult();\n} catch (CoercionException e) {\n    throw new BadRequestException(\"rate must be numeric\", e);\n}","preventionTips":["Convert web/JSON input to the target numeric type before Hibernate sees it","Avoid locale-sensitive number parsing in request handlers","Pass explicit types on native query parameters"],"tags":["hibernate","type-coercion","numeric","double","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"}