{"record":{"id":"6bc4dcbde13036ac","repo":"hibernate/hibernate-orm","slug":"cannot-coerce-long-value-s-as-short-overflow","errorCode":null,"errorMessage":"Cannot coerce Long value `%s` as Short : overflow","messagePattern":"Cannot coerce Long value `(.+?)` as Short : overflow","errorType":"exception","errorClass":"CoercionException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CoercionHelper.java","lineNumber":188,"sourceCode":"\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) {\n\t\tif ( ! isWholeNumber( doubleValue ) ) {\n\t\t\tthrow new CoercionException( \"Cannot coerce Double value `\" + doubleValue + \"` as Short : not a whole number\" );\n\t\t}\n\t\treturn toShort( doubleValue.longValue() );\n\t}\n\n\tpublic static Short toShort(Float floatValue) {\n\t\tif ( ! isWholeNumber( floatValue ) ) {","sourceCodeStart":170,"sourceCodeEnd":206,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CoercionHelper.java#L170-L206","documentation":"Hibernate throws this CoercionException when a Long is narrowed to Short (range -32768..32767) and exceeds 32767. Thrown from CoercionHelper.toShort(Long), which ShortJavaType.coerce calls when a Long value reaches a Short-mapped attribute. Hibernate 6 performs explicit bounds checks on every narrowing coercion, so oversized values fail with the offending number in the message instead of being truncated.","triggerScenarios":"`ShortJavaType.coerce(value)` with a Long > 32767: assigning a long variable to a Short field via a Number/Object-typed setter, `setParameter(\"s\", 70000L)` on a Short-typed path, or merging a detached entity whose Short property was loaded from a Long-valued JSON field.","commonSituations":"IDs/counts computed as long feeding SMALLINT-mapped Short fields; Jackson configured to deserialize ints as Long; unsigned SMALLINT columns (values up to 65535) on MySQL/MariaDB mapped as Short; migration from Hibernate 5 where oversized longs truncated silently.","solutions":["Widen the attribute type to Long/Integer if the domain legitimately exceeds the short range.","Explicitly range-check (-32768..32767) and cast `(short) longValue` where fit is guaranteed.","Fix producers that use Long for quantities that are actually small integers.","For unsigned SMALLINT data, map the attribute as Integer with an appropriate JDBC type."],"exampleFix":"// before\nLong visitCount = counterService.currentCount();  // e.g. 50000\nstats.setVisitCount(visitCount);                  // Short field -> overflow\n\n// after\nif (visitCount < Short.MIN_VALUE || visitCount > Short.MAX_VALUE) {\n    throw new IllegalArgumentException(\"visit count exceeds short range: \" + visitCount);\n}\nstats.setVisitCount(visitCount.shortValue());\n// or change visitCount attribute/column to Long/BIGINT","handlingStrategy":"validation","validationCode":"Long v = (Long) raw;\nif (v < Short.MIN_VALUE || v > Short.MAX_VALUE) throw new IllegalArgumentException(\"out of short range: \" + v);\nstats.setVisitCount(v.shortValue());","typeGuard":"static boolean fitsInShort(Long v) { return v != null && v >= (long) Short.MIN_VALUE && v <= (long) Short.MAX_VALUE; }","tryCatchPattern":"try { session.persist(stats); } catch (CoercionException e) { log and reject with field-level message; }","preventionTips":["Don't use Long for values bounded by SMALLINT.","Map unsigned SMALLINT columns (0..65535) as Integer.","Counter fields expected to grow should be Integer/Long from day one."],"tags":["hibernate","type-coercion","short","long","overflow"],"backgroundTag":"numeric-overflow-underflow","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}