{"record":{"id":"5dec9d2f3e0aadfc","repo":"hibernate/hibernate-orm","slug":"cannot-coerce-integer-value-s-as-short-underf","errorCode":null,"errorMessage":"Cannot coerce Integer value `%s` as Short : underflow","messagePattern":"Cannot coerce Integer value `(.+?)` as Short : underflow","errorType":"exception","errorClass":"CoercionException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CoercionHelper.java","lineNumber":180,"sourceCode":"\tpublic static Byte toByte(BigInteger value) {\n\t\treturn coerceWrappingError( value::byteValueExact );\n\t}\n\n\tpublic static Byte toByte(BigDecimal value) {\n\t\treturn coerceWrappingError( value::byteValueExact );\n\t}\n\n\tpublic static Short toShort(Byte value) {\n\t\treturn value.shortValue();\n\t}\n\n\tpublic static Short toShort(Integer value) {\n\t\tif ( value > Short.MAX_VALUE ) {\n\t\t\tthrow new CoercionException( \"Cannot coerce Integer value `\" + value + \"` as Short : overflow\" );\n\t\t}\n\n\t\tif ( value < Short.MIN_VALUE ) {\n\t\t\tthrow new CoercionException( \"Cannot coerce Integer value `\" + value + \"` as Short : underflow\" );\n\t\t}\n\n\t\treturn value.shortValue();\n\t}\n\n\tpublic static Short toShort(Long value) {\n\t\tif ( value > Short.MAX_VALUE ) {\n\t\t\tthrow new CoercionException( \"Cannot coerce Long value `\" + value + \"` as Short : overflow\" );\n\t\t}\n\n\t\tif ( value < Short.MIN_VALUE ) {\n\t\t\tthrow new CoercionException( \"Cannot coerce Long value `\" + value + \"` as Short : underflow\" );\n\t\t}\n\n\t\treturn value.shortValue();\n\t}\n\n\tpublic static Short toShort(Double doubleValue) {","sourceCodeStart":162,"sourceCodeEnd":198,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CoercionHelper.java#L162-L198","documentation":"Hibernate throws this CoercionException when an Integer is narrowed to Short and falls below -32768. It is thrown by CoercionHelper.toShort(Integer), reached through ShortJavaType.coerce during persist/merge or parameter binding on a Short-mapped attribute. It is the underflow counterpart of the overflow check in the same method, and exists because Hibernate 6 rejects lossy narrowing of numeric types.","triggerScenarios":"Assigning an Integer < -32768 (e.g. -40000) to a Short/short entity field through a loosely typed setter, merging entities whose Short property was populated from an Integer variable, or binding a negative int literal below -32768 to a Short-typed query parameter.","commonSituations":"Negative sentinels (-99999) used as status codes stored in SMALLINT fields; coordinate/offset math in int overflowing the short domain; int-typed constants from other libraries pushed into Short attributes; Hibernate 5 -> 6 upgrades exposing formerly silent truncation.","solutions":["Re-type the attribute (Short -> Integer) and column (SMALLINT -> INT) if the domain needs values below -32768.","Validate and narrow explicitly: range-check then `(short) value`.","Eliminate out-of-range sentinel constants; use a properly sized column for special codes.","Add boundary validation for any Number destined for Short fields before Session calls."],"exampleFix":"// before\nint signedOffset = computeSignedOffset();  // e.g. -70000\nregion.setOffset(signedOffset);            // Short 'offset' -> underflow\n\n// after\nif (signedOffset >= Short.MIN_VALUE && signedOffset <= Short.MAX_VALUE) {\n    region.setOffset((short) signedOffset);\n} else {\n    throw new IllegalArgumentException(\"offset out of short range: \" + signedOffset);\n}","handlingStrategy":"validation","validationCode":"if (intValue < Short.MIN_VALUE || intValue > Short.MAX_VALUE) {\n    throw new IllegalArgumentException(\"offset out of short range: \" + intValue);\n}\nregion.setOffset((short) intValue);","typeGuard":"static boolean fitsInShort(int v) { return v >= -32768 && v <= 32767; }","tryCatchPattern":"catch CoercionException and map to a 422 validation response; deterministic, no retry value.","preventionTips":["Ban out-of-range sentinel ints for smallint-mapped status fields.","Keep coordinate/offset math aware of the target column width.","Audit stored data before narrowing SMALLINT -> TINYINT style refactors."],"tags":["hibernate","type-coercion","short","underflow","numeric-range"],"backgroundTag":"numeric-overflow-underflow","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}