{"record":{"id":"e810be7662a3c641","repo":"hibernate/hibernate-orm","slug":"cannot-coerce-float-value-s-as-short-not-a-wh","errorCode":null,"errorMessage":"Cannot coerce Float value `%s` as Short : not a whole number","messagePattern":"Cannot coerce Float value `(.+?)` as Short : not a whole number","errorType":"exception","errorClass":"CoercionException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CoercionHelper.java","lineNumber":207,"sourceCode":"\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}\n\n\tpublic static Short toShort(BigInteger value) {\n\t\treturn coerceWrappingError( value::shortValueExact );\n\t}\n\n\tpublic static Short toShort(BigDecimal value) {\n\t\treturn coerceWrappingError( value::shortValueExact );\n\t}\n\n\tpublic static Integer toInteger(Byte value) {\n\t\treturn value.intValue();\n\t}\n\n\tpublic static Integer toInteger(Short value) {\n\t\treturn value.intValue();","sourceCodeStart":189,"sourceCodeEnd":225,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CoercionHelper.java#L189-L225","documentation":"Hibernate throws this CoercionException when a Float with a fractional part is coerced to Short. CoercionHelper.toShort(Float) checks isWholeNumber(floatValue) and fails on values like 1.5f. It is called from ShortJavaType.coerce when a Float value reaches a Short-mapped attribute during ORM operations (persist, merge, query parameter binding). This is Hibernate's fail-fast guard against silent lossy fractional-to-integral conversion.","triggerScenarios":"Assigning a Float such as 7.3f to a Short/short entity property: `product.setRating(uiRating)` where rating maps to SMALLINT, or `setParameter(\"s\", 4.5f)` against a Short-typed path; common with UI layers and math APIs that return Float.","commonSituations":"Float-based rating/progress/scale values from mobile or desktop UIs stored in SMALLINT fields; float math libraries feeding integral columns; API DTOs typed Float copied onto Short entities; legacy attribute conversions from Float to Short.","solutions":["Round and narrow deliberately: `(short) Math.round(f)` when fractional precision is disposable; else re-type the attribute to Float/BigDecimal.","Store fractional data in a decimal-capable column instead of SMALLINT.","Validate that incoming Numbers are whole before assigning to integral fields.","Where narrowing policy is fixed, encode it once in an AttributeConverter<Float,Short>."],"exampleFix":"// before\nFloat rating = reviewWidget.getStars();  // e.g. 4.5f\nproduct.setRating(rating);               // Short 'rating' -> \"not a whole number\"\n\n// after\nproduct.setRating((short) Math.round(rating));\n// or map rating as Float with a REAL/FLOAT column","handlingStrategy":"validation","validationCode":"Float f = widget.getStars();\nif (f != Math.rint(f)) throw new IllegalArgumentException(\"stars must be whole: \" + f);\nproduct.setRating((short) Math.round(f));","typeGuard":"static boolean isWhole(Float f) { return f != null && !f.isNaN() && f == Math.rint(f); }","tryCatchPattern":"catch CoercionException at service boundary -> 400 with field name; deterministic failure, no retry.","preventionTips":["Quantize UI float inputs to steps (e.g. half-stars) via int at the boundary, or store ratings as Float.","Keep rating columns FLOAT/DECIMAL if fractional ratings are a product requirement."],"tags":["hibernate","type-coercion","short","float","fractional"],"backgroundTag":"fractional-to-integer-coercion","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}