{"record":{"id":"dc32f379371615da","repo":"hibernate/hibernate-orm","slug":"cannot-coerce-integer-value-s-as-short-overfl","errorCode":null,"errorMessage":"Cannot coerce Integer value `%s` as Short : overflow","messagePattern":"Cannot coerce Integer 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":176,"sourceCode":"\n\t\treturn value.byteValue();\n\t}\n\n\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","sourceCodeStart":158,"sourceCodeEnd":194,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CoercionHelper.java#L158-L194","documentation":"Hibernate throws this CoercionException when an Integer must be narrowed to Short (range -32768..32767) and exceeds 32767. It comes from CoercionHelper.toShort(Integer), called by ShortJavaType.coerce when an Integer value is supplied to a Short-mapped attribute (typical SMALLINT column). Hibernate 6 validates narrowing and reports the offending value instead of silently truncating like a Java `(short)` cast.","triggerScenarios":"`ShortJavaType.coerce(value)` with an Integer > 32767: `entity.setCode(intValue)` on a Short field via a Number/Object-typed setter, `setParameter(\"s\", 100000)` bound to a Short path, or session.merge on a graph where the Short property holds an Integer (e.g. from JSON deserialization).","commonSituations":"Year 50000-style values, ports, or counters stored in SMALLINT-mapped Short fields; Jackson deserializing JSON ints as Integer into DTOs copied onto Short entities; unsigned SMALLINT columns on MySQL holding 32768..65535; migrating from Hibernate 5 which truncated silently.","solutions":["Widen the mapping if large values are legitimate: attribute Short -> Integer and column SMALLINT -> INT.","Narrow explicitly after validation if the domain guarantees fit: check -32768..32767 then `(short) intValue`.","Fix DTO/producer types so integral fields are Short/Integer consistently end to end.","For unsigned SMALLINT data, map to Integer with an appropriate JdbcType instead of Short."],"exampleFix":"// before\n@Entity class PortConfig {\n    Short port;                   // smallint column\n}\ncfg.setPort(80800);              // Integer 80800 -> CoercionException: overflow\n\n// after\n@Entity class PortConfig {\n    Integer port;                 // int column\n}\n// or guard:\nif (portValue < Short.MIN_VALUE || portValue > Short.MAX_VALUE) throw new IllegalArgumentException(\"port too large\");\ncfg.setPort((short) portValue);","handlingStrategy":"validation","validationCode":"Integer v = dto.getPort();\nif (v < Short.MIN_VALUE || v > Short.MAX_VALUE) throw new IllegalArgumentException(\"port out of short range: \" + v);\ncfg.setPort(v.shortValue());","typeGuard":"static boolean fitsInShort(Integer v) { return v != null && v >= Short.MIN_VALUE && v <= Short.MAX_VALUE; }","tryCatchPattern":"try { session.merge(cfg); } catch (CoercionException e) { throw new ValidationException(\"port value not representable as smallint\", e); }","preventionTips":["Use Integer/int for anything that can plausibly exceed 32767 (years+epoch parts, ports, large counters).","Type DTO numeric fields to match the entity field exactly.","Remember MySQL unsigned SMALLINT max is 65535 — map such columns to Integer, not Short.","Boundary tests at 32767/32768 and -32768/-32769."],"tags":["hibernate","type-coercion","short","overflow","numeric-range"],"backgroundTag":"numeric-overflow-underflow","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}