{"record":{"id":"70aaf85e39fd305c","repo":"hibernate/hibernate-orm","slug":"cannot-coerce-float-value-s-to-byte-not-a-who","errorCode":null,"errorMessage":"Cannot coerce Float value `%s` to Byte : not a whole number","messagePattern":"Cannot coerce Float value `(.+?)` to Byte : 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":130,"sourceCode":"\t\t\t);\n\t\t}\n\n\t\tif ( value < Byte.MIN_VALUE ) {\n\t\t\tthrow new CoercionException(\n\t\t\t\t\tString.format(\n\t\t\t\t\t\t\tLocale.ROOT,\n\t\t\t\t\t\t\t\"Cannot coerce Double value `%s` to Byte : underflow\",\n\t\t\t\t\t\t\tvalue\n\t\t\t\t\t)\n\t\t\t);\n\t\t}\n\n\t\treturn value.byteValue();\n\t}\n\n\tpublic static Byte toByte(Float value) {\n\t\tif ( ! isWholeNumber( value ) ) {\n\t\t\tthrow new CoercionException(\n\t\t\t\t\tString.format(\n\t\t\t\t\t\t\tLocale.ROOT,\n\t\t\t\t\t\t\t\"Cannot coerce Float value `%s` to Byte : not a whole number\",\n\t\t\t\t\t\t\tvalue\n\t\t\t\t\t)\n\t\t\t);\n\t\t}\n\n\t\tif ( value > Byte.MAX_VALUE ) {\n\t\t\tthrow new CoercionException(\n\t\t\t\t\tString.format(\n\t\t\t\t\t\t\tLocale.ROOT,\n\t\t\t\t\t\t\t\"Cannot coerce Float value `%s` to Byte : overflow\",\n\t\t\t\t\t\t\tvalue\n\t\t\t\t\t)\n\t\t\t);\n\t\t}\n","sourceCodeStart":112,"sourceCodeEnd":148,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CoercionHelper.java#L112-L148","documentation":"Hibernate throws this CoercionException when a Float with a fractional part is coerced to Byte. CoercionHelper.toByte(Float) runs isWholeNumber(value) first; values like 2.5f fail immediately. It is invoked from ByteJavaType.coerce when a Float value reaches a Byte-mapped attribute during persist/merge or parameter binding. Hibernate refuses lossy fractional-to-integral conversion rather than truncating.","triggerScenarios":"`ByteJavaType.coerce(2.5f)` — assigning a Float literal or computed float to a Byte/byte entity property, or `setParameter(\"b\", 1.1f)` against a Byte path; typical when math libraries or UI layers hand back Floats for integral code fields.","commonSituations":"Android/Swing UI sliders returning Float progress into tinyint Byte fields; float-based math (weights 0.5f multipliers) feeding code columns; legacy APIs typed as Float; refactor of an attribute from Float to Byte while call sites still pass floats.","solutions":["Round and narrow explicitly when truncation is fine: `(byte) Math.round(f)` after a range check; otherwise change the attribute type to Float.","Fix the producer to emit integral types (Integer/Byte) for integral fields.","Validate incoming Numbers at the boundary: whole-number plus range checks before Session calls.","Consider an AttributeConverter<Float,Byte> to centralize rounding policy."],"exampleFix":"// before\nfloat progress = uiSlider.getValue();   // e.g. 2.5f\njob.setProgress(progress);              // Byte 'progress' -> \"not a whole number\"\n\n// after\njob.setProgress((byte) Math.round(progress));\n// or declare progress as Float if fractional progress is meaningful","handlingStrategy":"validation","validationCode":"Float f = uiValue;\nif (f != Math.rint(f)) throw new IllegalArgumentException(\"value must be whole: \" + f);\nif (f < Byte.MIN_VALUE || f > Byte.MAX_VALUE) throw new IllegalArgumentException(\"out of byte range: \" + f);\nentity.setCode((byte) (int) f);","typeGuard":"static boolean isWhole(Float f) { return f != null && !f.isNaN() && !f.isInfinite() && f == Math.rint(f); }","tryCatchPattern":"catch CoercionException at the service boundary and translate to a 400 with the field name; deterministic — never retry.","preventionTips":["Convert UI float outputs (sliders, gauges) to int at the presentation boundary.","Keep integral entity fields typed byte/short/int end to end; don't accept Number in setters.","Add tests with 0.5f increments for integral fields."],"tags":["hibernate","type-coercion","byte","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"}