{"record":{"id":"24a6e9020b602dfe","repo":"hibernate/hibernate-orm","slug":"cannot-coerce-float-value-s-to-byte-overflow","errorCode":null,"errorMessage":"Cannot coerce Float value `%s` to Byte : overflow","messagePattern":"Cannot coerce Float value `(.+?)` to Byte : overflow","errorType":"exception","errorClass":"CoercionException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CoercionHelper.java","lineNumber":140,"sourceCode":"\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\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 Float 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","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CoercionHelper.java#L122-L158","documentation":"Hibernate throws this CoercionException when a whole-number Float is narrowed to Byte and exceeds 127. In CoercionHelper.toByte(Float), after the isWholeNumber guard, `value > Byte.MAX_VALUE` fails (byte constant widened to float for the comparison). Reached via ByteJavaType.coerce for Float values on Byte-mapped attributes. It is a hard, deterministic mapping failure in Hibernate 6's stricter coercion model.","triggerScenarios":"Assigning a whole Float > 127 (e.g. 200.0f, 1e3f) to a Byte/byte entity field or binding it against a Byte-typed query parameter; e.g. `device.setPowerLevel(measuredFloat)` with powerLevel mapped from TINYINT.","commonSituations":"Sensor readings in float form feeding tinyint-mapped Byte fields; ADC values (0..1023.0f) stored into a byte column by mistake; ports/page sizes computed as Float; Hibernate 5 -> 6 migration surfacing previously truncated values.","solutions":["Widen the attribute and column if values legitimately exceed 127 (Byte -> Short/Integer, TINYINT -> SMALLINT).","Narrow explicitly with validation: `int i = Math.round(f); if (i > 127 || i < -128) throw ...; (byte) i`.","Fix upstream float producers to use integral types.","Centralize intentional narrowing in an AttributeConverter."],"exampleFix":"// before\nFloat adc = sensorReader.readAdc();    // e.g. 512.0f\npin.setThreshold(adc);                 // Byte 'threshold' -> overflow\n\n// after\nprivate static byte toByteRange(Float f) {\n    int i = Math.round(f);\n    if (i < Byte.MIN_VALUE || i > Byte.MAX_VALUE) throw new IllegalArgumentException(\"ADC out of byte range: \" + i);\n    return (byte) i;\n}\npin.setThreshold(toByteRange(adc));\n// or map threshold as Integer with an INT column","handlingStrategy":"validation","validationCode":"int i = Math.round(adcFloat);\nif (i < Byte.MIN_VALUE || i > Byte.MAX_VALUE) throw new IllegalArgumentException(\"ADC out of byte range: \" + i);\npin.setThreshold((byte) i);","typeGuard":"static boolean fitsInByte(Float f) { return f != null && f == Math.rint(f) && f >= -128f && f <= 127f; }","tryCatchPattern":"try { session.persist(pin); } catch (CoercionException e) { throw new IllegalArgumentException(\"threshold not representable as byte\", e); }","preventionTips":["Map ADC ranges wider than ±127 to Integer/Short columns.","Clamp sensor readings to the column domain before persistence.","Smoke-test with max-range sensor values in staging."],"tags":["hibernate","type-coercion","byte","float","overflow"],"backgroundTag":"numeric-overflow-underflow","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}