hibernate/hibernate-orm · error · CoercionException
value must contain exactly one character: '" + string + "'
Error message
value must contain exactly one character: '" + string + "'
What it means
CharacterJavaType.fromString requires a CharSequence of length exactly 1; an empty string or a multi-character sequence throws CoercionException('value must contain exactly one character'). It is the parsing path used when a Character attribute is hydrated from string data.
Source
Thrown at hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CharacterJavaType.java:38
public CharacterJavaType() {
super( Character.class );
}
@Override
public boolean useObjectEqualsHashCode() {
return true;
}
@Override
public String toString(Character value) {
return value.toString();
}
@Override
public Character fromString(CharSequence string) {
if ( string.length() != 1 ) {
throw new CoercionException( "value must contain exactly one character: '" + string + "'" );
}
return string.charAt( 0 );
}
@Override
public boolean isInstance(Object value) {
return value instanceof Character;
}
@Override
public Character cast(Object value) {
return (Character) value;
}
@Override
public <X> X unwrap(Character value, Class<X> type, WrapperOptions options) {
if ( value == null ) {
return null;View on GitHub (pinned to fad1729dce)
Solutions
- Use CHAR(1)/NCHAR(1) and make sure stored values are exactly one character.
- Clean the data: UPDATE t SET c = SUBSTRING(c FROM 1 FOR 1) WHERE CHAR_LENGTH(c) <> 1, and handle empty rows explicitly.
- Add a converter that trims and defaults empty input to a chosen character.
Example fix
-- before
UPDATE users SET initial = 'AB'; -- later hydrate throws CoercionException
-- after
UPDATE users SET initial = SUBSTRING(initial FROM 1 FOR 1) WHERE CHAR_LENGTH(initial) > 1;
DELETE-fallback or converter for '' rows:
@Converter
public class CharConverter implements AttributeConverter<Character, String> {
public String convertToDatabaseColumn(Character c) { return c == null ? null : c.toString(); }
public Character convertToEntityAttribute(String s) { return (s == null || s.isEmpty()) ? ' ' : s.trim().charAt(0); }
} Defensive patterns
Strategy: validation
Validate before calling
static boolean isSingleChar(String s) {
return s != null && s.length() == 1;
}
// guard: if (!isSingleChar(col)) col = normalize(col); // trim or default Type guard
static Character toCharOrNull(String s) {
if (s == null || s.isEmpty()) return null;
String t = s.trim();
return t.isEmpty() ? null : Character.valueOf(t.charAt(0));
} Prevention
- Use CHAR(1)/NCHAR(1) columns for Character attributes.
- Trim and validate form input before persisting to Character fields.
- Clean padded/legacy data when refactoring String columns to char(1).
When it happens
Trigger: A CHAR(n)/VARCHAR column feeding a Character attribute with values longer or shorter than one character; empty strings; string parameters bound to Character fields.
Common situations: DB CHAR columns whose padding survives the read (no trim) so values come back longer than 1; schema evolved from a wider string column without cleaning data; NOT NULL string columns defaulted to ''.
Related errors
- Cannot convert Character value '" + character + "' to Boolea
- value contains more than one character: '" + string + "'
- The INSERT statement for table [%s] contains no column, and
- cannot recreate collection while filter is enabled: " + coll
- cannot recreate collection while filter is enabled [%s : %s]
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/5fc29037a42be019.
Report an issue: GitHub.