{"record":{"id":"c3783af8f4553456","repo":"hibernate/hibernate-orm","slug":"the-string-is-not-a-valid-string-representation-of","errorCode":null,"errorMessage":"The string is not a valid string representation of a binary content.","messagePattern":"The string is not a valid string representation of a binary content\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ByteArrayJavaType.java","lineNumber":96,"sourceCode":"\t@Override\n\tpublic String toString(Byte[] bytes) {\n\t\tfinal var string = new StringBuilder();\n\t\tfor ( Byte aByte : bytes ) {\n\t\t\tfinal String hexStr = toHexString( toUnsignedInt( aByte ) );\n\t\t\tif ( hexStr.length() == 1 ) {\n\t\t\t\tstring.append( '0' );\n\t\t\t}\n\t\t\tstring.append( hexStr );\n\t\t}\n\t\treturn string.toString();\n\t}\n\t@Override\n\tpublic Byte[] fromString(CharSequence string) {\n\t\tif ( string == null ) {\n\t\t\treturn null;\n\t\t}\n\t\tif ( string.length() % 2 != 0 ) {\n\t\t\tthrow new IllegalArgumentException( \"The string is not a valid string representation of a binary content.\" );\n\t\t}\n\t\tfinal var bytes = new Byte[string.length() / 2];\n\t\tfor ( int i = 0; i < bytes.length; i++ ) {\n\t\t\tfinal String hexStr = string.subSequence( i * 2, (i + 1) * 2 ).toString();\n\t\t\tbytes[i] = (byte) Integer.parseInt( hexStr, 16 );\n\t\t}\n\t\treturn bytes;\n\t}\n\n\t@Override\n\tpublic <X> X unwrap(Byte[] value, Class<X> type, WrapperOptions options) {\n\t\tif ( value == null ) {\n\t\t\treturn null;\n\t\t}\n\t\tif ( Byte[].class.isAssignableFrom( type ) ) {\n\t\t\treturn type.cast( value );\n\t\t}\n\t\tif ( byte[].class.isAssignableFrom( type ) ) {","sourceCodeStart":78,"sourceCodeEnd":114,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ByteArrayJavaType.java#L78-L114","documentation":"ByteArrayJavaType.fromString decodes a Byte[] from the hex string produced by toString(), where every byte is exactly two hex digits. An input of odd length cannot be split into byte pairs and throws IllegalArgumentException('The string is not a valid string representation of a binary content.').","triggerScenarios":"A hex string of odd length in the column bound to a Byte[] attribute; hex truncated by manual edits or an external import; values that lost a leading zero during concatenation or formatting.","commonSituations":"Data-migration truncation; ETL steps that strip leading zeros; hand-edited seed rows; third-party systems writing hex with variable padding.","solutions":["Repair the data so every value has even length.","Normalize before the value reaches Hibernate: left-pad with '0' when length is odd.","Prefer byte[] with a native varbinary mapping over hex-in-varchar columns."],"exampleFix":"// before\nString hex = readLegacyColumn(); // e.g. \"A0F\" (3 chars) -> IllegalArgumentException\nByte[] data = ByteArrayJavaType.INSTANCE.fromString(hex);\n\n// after\nString hex = readLegacyColumn();\nif (hex.length() % 2 != 0) hex = \"0\" + hex; // \"0A0F\"\nByte[] data = ByteArrayJavaType.INSTANCE.fromString(hex);","handlingStrategy":"validation","validationCode":"static boolean isValidHex(String s) {\n    return s != null && s.length() % 2 == 0 && s.matches(\"[0-9a-fA-F]*\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Left-pad odd-length hex before assignment.","Use varbinary columns + byte[] mappings instead of hex text.","Reject non-hex characters at the ingest boundary."],"tags":["hibernate","hex","byte-array","parsing"],"backgroundTag":"hex-string-decode","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}