1

I'm trying to persist a Boolean column with [spring-data-jdbc] but conversion defaults to 'TRUE', 'FALSE' values, but database columns is char(1) ('T'/'F'):

2018-07-13 14:58:32.761 [main] DEBUG org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator - Translating SQLException with SQL state '22001', error code '22001', message [Valor demasiado largo para la columna "FLAG CHAR(1) NOT NULL": "'FALSE' (5)"
Value too long for column "OCCUPATION_FLAG CHAR(1) NOT NULL": "'FALSE' (5)"; SQL statement:
INSERT INTO MY_TABLE (description, flag, code) VALUES (?, ?, ?) [22001-197]]; SQL was [INSERT INTO MY_TABLE (description, flag, code) VALUES (?, ?, ?)] for task [PreparedStatementCallback]

I'm trying to create some converters, but still failing.

@Bean
public Converter<Boolean, Character> booleanToCharacter() {
    return new Converter<Boolean, Character>() {

        @Override
        public Character convert(Boolean source) {
            if (null==source) { return (Character)null;}
            return source.toString().toUpperCase().charAt(0);
        }
    };
}

@Bean
public Converter<Character, Boolean> characterToBoolean() {
    return new Converter<Character, Boolean>() {

        @Override
        public Boolean convert(Character source) {
            if (null==source) { return (Boolean)null; };
            return 'T'==Character.toUpperCase(source);
        }

    };
}

Any suggestion will be useful. Thanks.

  • Better use `TINYINT(1)` that allow `0` as `false` and `1` as `true`. To give you a more detailed help, please provide us the statement execution code. – Nikolas Charalambidis Jul 13 '18 at 21:01
  • Can you also post your entity class where you are using coverter ? – Amith Kumar Jul 13 '18 at 21:11
  • The upcoming 1.0.0.RC1 will have a fresh custom converter infrastructure https://jira.spring.io/browse/DATAJDBC-235. But then currently the JDBC driver converts the String back to boolean, which will be fixable once this is in place: https://jira.spring.io/browse/DATAJDBC-239 – Jens Schauder Jul 23 '18 at 06:24
  • Thanks, finally I modify my entity to store a Character instead of Boolean. public final Boolean getFlag() { return Character.valueOf('T').equals(flag); } public final void setFlag(boolean flag) { this.flag = (flag ? Character.valueOf('T') : Character.valueOf('F')); } – user3851443 Jul 23 '18 at 20:26

0 Answers0