{"record":{"id":"b7ff37c69852c956","repo":"hibernate/hibernate-orm","slug":"cannot-coerce-value-s-s-to-short","errorCode":null,"errorMessage":"Cannot coerce value '%s' [%s] to Short","messagePattern":"Cannot coerce value '(.+?)' \\[(.+?)\\] to Short","errorType":"exception","errorClass":"CoercionException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ShortJavaType.java","lineNumber":154,"sourceCode":"\n\t@Override\n\tpublic int getDefaultSqlPrecision(Dialect dialect, JdbcType jdbcType) {\n\t\treturn 5;\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 Short 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 Short\",\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 Short coerceOrNull(@Nonnull Object value) {\n\t\tif ( value instanceof Short shortValue ) {\n\t\t\treturn shortValue;\n\t\t}\n\n\t\tif ( value instanceof Byte byteValue ) {","sourceCodeStart":136,"sourceCodeEnd":172,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ShortJavaType.java#L136-L172","documentation":"ShortJavaType.coerce() runs when Hibernate must convert a bound or read value into a Short (parameter binding, result coercion). It calls coerceOrNull (numeric widening/narrowing, numeric strings) and throws CoercionException naming the value and its class when no conversion exists. The value's runtime type is fundamentally unconvertible to Short - it is not merely an overflow problem.","triggerScenarios":"query.setParameter(\"n\", someObject) where the parameter implies Short but the argument is an enum, date, or arbitrary object; native query results mapped to Short attributes returning non-numeric column types; criteria expressions or DB function results coerced to Short","commonSituations":"Refactors that changed a DTO field type while callers still pass the old object; native queries against views where the column type changed; non-numeric strings bound to Short parameters","solutions":["Pass a numeric value or numeric string; convert explicitly with ((Number) v).shortValue() before binding","For native queries, CAST the selected expression in SQL or remap the attribute to the actual column type","Bind with an explicit TypedParameterValue to avoid implicit coercion of the wrong runtime type","Validate inputs before binding (e.g. Number instance check or \\d+ regex for strings)"],"exampleFix":"// before\nquery.setParameter(\"code\", codeDto); // CoercionException: Cannot coerce value '...' to Short\n\n// after\nquery.setParameter(\"code\", codeDto.getCode().shortValue());","handlingStrategy":"type-guard","validationCode":"static boolean coercibleToShort(Object v) {\n    if (v == null || v instanceof Number) return true;\n    if (v instanceof String s) return s.matches(\"[+-]?\\d+\");\n    return false;\n}\n\nif (!coercibleToShort(param)) throw new IllegalArgumentException(\"Not coercible to Short: \" + param);","typeGuard":"static Short toShortOrNull(Object v) {\n    if (v instanceof Number n) { long l = n.longValue(); return (l >= Short.MIN_VALUE && l <= Short.MAX_VALUE) ? (short) l : null; }\n    if (v instanceof String s && s.matches(\"[+-]?\\d+\")) return Short.valueOf(s);\n    return null;\n}","tryCatchPattern":"try { Short bound = ShortJavaType.INSTANCE.coerce(value); }\ncatch (CoercionException e) {\n    throw new IllegalArgumentException(\"Parameter is not numeric: cannot bind to Short\", e);\n}","preventionTips":["Match the bound value's runtime type to the attribute/parameter type","Use TypedParameterValue for ambiguous bindings","CAST columns in native SQL so values arrive as numbers"],"tags":["hibernate","type-coercion","query-parameter","native-query","short"],"backgroundTag":"type-coercion-failure","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}