diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CharacterJavaType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CharacterJavaType.java index c4564f73d5de..36e2295fbf59 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CharacterJavaType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CharacterJavaType.java @@ -36,7 +36,7 @@ public String toString(Character value) { @Override public Character fromString(CharSequence string) { if ( string.length() != 1 ) { - throw new HibernateException( "multiple or zero characters found parsing string" ); + throw new CoercionException( "value must contain exactly one character: '" + string + "'" ); } return string.charAt( 0 ); } @@ -68,12 +68,10 @@ public Character wrap(X value, WrapperOptions options) { return character; } if (value instanceof String string) { - // Note that this conversion is "dangerous", - // since it is not invertible, and can in - // principle result in accidental loss of data. - // It might be better to throw if the incoming - // string does not have length one. - return string.isEmpty() ? null : string.charAt( 0 ); + if ( string.length() != 1 ) { + throw new CoercionException( "value must contain exactly one character: '" + string + "'" ); + } + return string.charAt( 0 ); } if (value instanceof Number number) { return (char) number.shortValue(); diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CoercionException.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CoercionException.java index bce67bd0c349..e683d1674eda 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CoercionException.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CoercionException.java @@ -7,6 +7,8 @@ import org.hibernate.HibernateException; /** + * A problem converting between JDBC types and Java types. + * * @author Steve Ebersole */ public class CoercionException extends HibernateException {