Skip to content

Commit

Permalink
throw CoercionException when CharacterJavaType receives a string of w…
Browse files Browse the repository at this point in the history
…rong length

Signed-off-by: Gavin King <gavin@hibernate.org>
  • Loading branch information
gavinking committed Oct 30, 2024
1 parent 107524c commit 6bdcabb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
Expand Down Expand Up @@ -68,12 +68,10 @@ public <X> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 6bdcabb

Please sign in to comment.