{"record":{"id":"885c64ef5fc8dfc6","repo":"hibernate/hibernate-orm","slug":"cannot-coerce-value-s-s-to-integer","errorCode":null,"errorMessage":"Cannot coerce value '%s' [%s] to Integer","messagePattern":"Cannot coerce value '(.+?)' \\[(.+?)\\] to Integer","errorType":"exception","errorClass":"CoercionException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/type/descriptor/java/IntegerJavaType.java","lineNumber":163,"sourceCode":"\n\t@Override\n\tpublic int getDefaultSqlPrecision(Dialect dialect, JdbcType jdbcType) {\n\t\treturn 10;\n\t}\n\n\t@Override\n\tpublic int getDefaultSqlScale(Dialect dialect, JdbcType jdbcType) {\n\t\treturn 0;\n\t}\n\n\t@Override\n\tpublic @Nullable Integer 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 Integer\",\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 Integer coerceOrNull(@Nonnull Object value) {\n\t\tif ( value instanceof Integer integer ) {\n\t\t\treturn integer;\n\t\t}\n\n\t\tif ( value instanceof Short shortValue ) {","sourceCodeStart":145,"sourceCodeEnd":181,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/IntegerJavaType.java#L145-L181","documentation":"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.","triggerScenarios":"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.","commonSituations":"Web/JSON input passed unparsed to setParameter; enum or boolean values reaching an int field via a bad mapping; locale-formatted numeric strings.","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"],"exampleFix":"// before\nq.setParameter(\"limit\", request.getParameter(\"limit\")); // \"20items\" -> CoercionException\n\n// after\nq.setParameter(\"limit\", Integer.parseInt(request.getParameter(\"limit\").trim()));","handlingStrategy":"type-guard","validationCode":"static Integer toIntegerOrNull(Object v) {\n    try {\n        return v instanceof Number n ? n.intValue()\n                : Integer.parseInt(String.valueOf(v).trim());\n    } catch (RuntimeException e) {\n        return null;\n    }\n}","typeGuard":"static boolean coercibleToInteger(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]+\");\n}","tryCatchPattern":"try {\n    return q.setParameter(\"limit\", value).getSingleResult();\n} catch (CoercionException e) {\n    throw new BadRequestException(\"limit must be an integer\", e);\n}","preventionTips":["Parse and validate integer input at the request boundary","Reject fractional strings for int fields early","Pass explicit parameter types on queries"],"tags":["hibernate","type-coercion","numeric","integer","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"}