{"record":{"id":"24a105e42268b59e","repo":"hibernate/hibernate-orm","slug":"cannot-coerce-long-value-s-as-short-underflow","errorCode":null,"errorMessage":"Cannot coerce Long value `%s` as Short : underflow","messagePattern":"Cannot coerce Long 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":192,"sourceCode":"\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 ) ) {\n\t\t\tthrow new CoercionException( \"Cannot coerce Float value `\" + floatValue + \"` as Short : not a whole number\" );\n\t\t}\n\t\treturn toShort( floatValue.longValue() );\n\t}","sourceCodeStart":174,"sourceCodeEnd":210,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CoercionHelper.java#L174-L210","documentation":"Hibernate throws this CoercionException when a Long is narrowed to Short and falls below -32768. It originates in CoercionHelper.toShort(Long), invoked by ShortJavaType.coerce when a Long value is coerced for a Short-mapped attribute. Together with its overflow sibling it enforces Hibernate 6's no-lossy-narrowing policy; the message names the exact value that failed.","triggerScenarios":"Assigning a Long < -32768 (e.g. -100000L) to a Short/short entity attribute via a loosely typed setter, or binding it as a query parameter to a Short-typed path; e.g. `account.setBalanceDelta(-100000L)` with balanceDelta mapped from SMALLINT.","commonSituations":"Long-typed deltas in financial or telemetry pipelines stored into SMALLINT fields; negative sentinels or error codes as Long constants; Hibernate 5 -> 6 upgrades where the same write used to truncate to a wrong-but-silent value; dynamic Map-based entity models with Long values.","solutions":["Re-type attribute/column (Short -> Long/Integer, SMALLINT -> INT/BIGINT) to match real magnitudes.","Range-check and narrow explicitly: `-32768 <= v <= 32767` then `(short) v`.","Fix upstream sign/unit bugs that inflate magnitudes beyond the short domain.","Validate Long inputs against the short range at the API/import boundary."],"exampleFix":"// before\nlong delta = oldBalance - newBalance;  // e.g. -150000\naccount.setBalanceDelta(delta);        // Short field -> underflow\n\n// after\nif (delta < Short.MIN_VALUE || delta > Short.MAX_VALUE) {\n    throw new IllegalArgumentException(\"balance delta out of short range: \" + delta);\n}\naccount.setBalanceDelta((short) delta);\n// or widen balanceDelta to Long","handlingStrategy":"validation","validationCode":"if (delta < Short.MIN_VALUE || delta > Short.MAX_VALUE) {\n    throw new IllegalArgumentException(\"delta out of short range: \" + delta);\n}\naccount.setBalanceDelta((short) delta);","typeGuard":"static boolean fitsInShort(long v) { return v >= -32768L && v <= 32767L; }","tryCatchPattern":"catch CoercionException around merge/persist; surface as business-rule violation, never swallow silently.","preventionTips":["Model balance/telemetry deltas with the same width as their column.","Validate at the pipeline entry (Kafka/HTTP consumer), not at the ORM layer."],"tags":["hibernate","type-coercion","short","long","underflow"],"backgroundTag":"numeric-overflow-underflow","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}