无法使用 spring-jdbc NamedParameterJdbcTemplate 以批处理方式存储 Enum

Unable to store Enum in batch way with spring-jdbc NamedParameterJdbcTemplate

我遇到了 spring-jdbc 的枚举问题。我有一个包含枚举值的 POJO。我想将字符串值存储在数据库中,而不是序号。我制作了一个 DAO,它工作得很好,除了带有 namedParameterJdbcTemplate 的批处理方法(对于 jdbcTemplate 和 BatchPreparedStatementSetter 它可以工作,但我更喜欢使用命名参数)。

例如:

public int[] batchUpdate(List<MyPojo> pojos) throws DaoException {
    SqlParameterSource[] parameters = new SqlParameterSource[pojos.size()];

    for (int i = 0; i < pojos.size(); i++) {
        parameters[i] = new BeanPropertySqlParameterSource(pojos.get(i));
    }

    try {
        return namedParameterJdbcTemplate.batchUpdate(SQL_UPDATE, (SqlParameterSource[]) parameters);
    } catch (Exception ex) {
        throw new DaoException(ex);
    }
}

不适用于枚举属性。

我有这个错误:

Caused by: org.h2.jdbc.JdbcSQLException: Value too long for column "STATUS CHARACTER VARYING(20) NOT NULL": "'aced00057e7200466f72672e67656e792e7064702e6669726562697264732e67656e79746f74652e646f6d61696e6d6f64656c2e6265742e4265744465636f6... (258)"; SQL statement: UPDATE ... SET ..., status = ? WHERE id = ? [22001-187] at org.h2.message.DbException.getJdbcSQLException(DbException.java:345) at org.h2.message.DbException.get(DbException.java:179) at org.h2.table.Column.validateConvertUpdateSequence(Column.java:327) at org.h2.table.Table.validateConvertUpdateSequence(Table.java:737) at org.h2.command.dml.Update.update(Update.java:125) at org.h2.command.CommandContainer.update(CommandContainer.java:78) at org.h2.command.Command.executeUpdate(Command.java:254) at org.h2.jdbc.JdbcPreparedStatement.executeUpdateInternal(JdbcPreparedStatement.java:157) at org.h2.jdbc.JdbcPreparedStatement.executeBatch(JdbcPreparedStatement.java:1183) at org.springframework.jdbc.core.JdbcTemplate.doInPreparedStatement(JdbcTemplate.java:1005) at org.springframework.jdbc.core.JdbcTemplate.doInPreparedStatement(JdbcTemplate.java:989) at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:644) ... 43 more

MyPojo 有一个枚举值(状态)。这很奇怪,因为我要存储的枚举字符串是 'validated' 而不是 'aced000...'。如果我使用的是 JPA,我会使用 @Enumerated(EnumType.STRING) 作为我的状态字段,但是 spring-jdbc 上是否存在这样的东西?

此致

最后通过注册sql类型解决:

    for (int i = 0; i < bets.size(); i++) {
        BeanPropertySqlParameterSource bpsps = new BeanPropertySqlParameterSource(pojos.get(i));
        bpsps.registerSqlType("status", Types.VARCHAR);
        parameters[i] = bpsps;
    }