{"record":{"id":"b309ad77322704de","repo":"hibernate/hibernate-orm","slug":"value-contains-more-than-one-character-strin","errorCode":null,"errorMessage":"value contains more than one character: '\" + string + \"'","messagePattern":"value contains more than one character: '\" \\+ string \\+ \"'","errorType":"exception","errorClass":"CoercionException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CharacterJavaType.java","lineNumber":92,"sourceCode":"\t\t}\n\t\telse if (value instanceof Character character) {\n\t\t\treturn character;\n\t\t}\n\t\telse if (value instanceof String string) {\n\t\t\tswitch ( string.length() ) {\n\t\t\t\tcase 1:\n\t\t\t\t\treturn string.charAt( 0 );\n\t\t\t\tcase 0:\n\t\t\t\t\tif ( options.getDialect().stripsTrailingSpacesFromChar() ) {\n\t\t\t\t\t\t// we previously stored char values in char(1) columns on MySQL\n\t\t\t\t\t\t// but MySQL strips trailing spaces from the value when read\n\t\t\t\t\t\treturn ' ';\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tthrow new CoercionException( \"value does not contain a character: '\" + string + \"'\" );\n\t\t\t\t\t}\n\t\t\t\tdefault:\n\t\t\t\t\tthrow new CoercionException( \"value contains more than one character: '\" + string + \"'\" );\n\t\t\t}\n\t\t}\n\t\telse if (value instanceof Number number) {\n\t\t\treturn (char) number.shortValue();\n\t\t}\n\t\telse {\n\t\t\tthrow unknownWrap( value.getClass() );\n\t\t}\n\t}\n\n\t@Override\n\tpublic Class<?> getPrimitiveClass() {\n\t\treturn char.class;\n\t}\n\n\t@Override\n\tpublic Class<Character[]> getArrayClass() {\n\t\treturn Character[].class;","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CharacterJavaType.java#L74-L110","documentation":"CharacterJavaType.wrap throws CoercionException('value contains more than one character') when the incoming String is longer than one character — a Character attribute physically cannot hold more. This is the length>1 branch of the same switch that handles the empty-string case.","triggerScenarios":"A varchar column with multi-character values feeding a Character attribute; native queries aliasing a wider column into a char(1) field; code setting a String into a Character property via the wrap path.","commonSituations":"Schema drift (column widened or reused for codes); joined lookup tables with 2-3 char codes; data entry that slipped past application validation.","solutions":["Fix the column to char(1) and clean existing data to single characters.","If truncation is intended, add a converter that explicitly takes the first character.","If the values are legitimately longer, change the attribute to String."],"exampleFix":"-- before\nALTER TABLE users ALTER COLUMN initial TYPE varchar(3); -- values 'AB' appear\n@Basic private Character initial; // wrap(\"AB\") -> CoercionException\n\n-- after\nUPDATE users SET initial = SUBSTRING(initial FROM 1 FOR 1);\nALTER TABLE users ALTER COLUMN initial TYPE char(1);","handlingStrategy":"validation","validationCode":"static boolean fitsCharacter(String s) {\n    return s == null || s.length() <= 1;\n}\n// reject or truncate explicitly: if (s.length() > 1) throw ...;","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Add a length-1 CHECK/CHAR(1) constraint at the database level.","Validate lengths in the UI/service layer for Character fields.","When a code column widens, update the entity mapping in the same change."],"tags":["hibernate","character","mapping","data-length"],"backgroundTag":"character-length-mapping","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}