treat an empty string as a null character in CharacterJavaType conversion

an empty string should not be converted to a space char

Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
Gavin King 2024-10-30 10:28:47 +01:00
parent 26988dd536
commit 2924fe8875
1 changed files with 7 additions and 6 deletions

View File

@ -67,12 +67,13 @@ public class CharacterJavaType extends AbstractClassJavaType<Character> implemen
if (value instanceof Character character) { if (value instanceof Character character) {
return character; return character;
} }
if ( value instanceof String ) { if (value instanceof String string) {
if ( value.equals( "" ) ) { // Note that this conversion is "dangerous",
return ' '; // since it is not invertible, and can in
} // principle result in accidental loss of data.
final String str = (String) value; // It might be better to throw if the incoming
return str.charAt( 0 ); // string does not have length one.
return string.isEmpty() ? null : string.charAt( 0 );
} }
if (value instanceof Number number) { if (value instanceof Number number) {
return (char) number.shortValue(); return (char) number.shortValue();