Skip to content

Commit 920d4bc

Browse files
committed
throw CoercionException when CharacterJavaType receives a string of wrong length
Signed-off-by: Gavin King <[email protected]>
1 parent 7df56da commit 920d4bc

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CharacterJavaType.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public String toString(Character value) {
3636
@Override
3737
public Character fromString(CharSequence string) {
3838
if ( string.length() != 1 ) {
39-
throw new HibernateException( "multiple or zero characters found parsing string" );
39+
throw new CoercionException( "value must contain exactly one character: '" + string + "'" );
4040
}
4141
return string.charAt( 0 );
4242
}
@@ -68,12 +68,10 @@ public <X> Character wrap(X value, WrapperOptions options) {
6868
return character;
6969
}
7070
if (value instanceof String string) {
71-
// Note that this conversion is "dangerous",
72-
// since it is not invertible, and can in
73-
// principle result in accidental loss of data.
74-
// It might be better to throw if the incoming
75-
// string does not have length one.
76-
return string.isEmpty() ? null : string.charAt( 0 );
71+
if ( string.length() != 1 ) {
72+
throw new CoercionException( "value must contain exactly one character: '" + string + "'" );
73+
}
74+
return string.charAt( 0 );
7775
}
7876
if (value instanceof Number number) {
7977
return (char) number.shortValue();

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/CoercionException.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import org.hibernate.HibernateException;
88

99
/**
10+
* A problem converting between JDBC types and Java types.
11+
*
1012
* @author Steve Ebersole
1113
*/
1214
public class CoercionException extends HibernateException {

0 commit comments

Comments
 (0)