spring批量导入期间枚举到字符串的转换失败

pxy2qtax  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(388)

我正在使用springboot和hiberna创建一个spring批处理作业,但是在插入过程中遇到了一个问题。这是代码的相关部分:

@Bean
    public JdbcBatchItemWriter<OphthalmicLens> writer(DataSource dataSource) throws SQLException {
        return new JdbcBatchItemWriterBuilder<OphthalmicLens>()
                .itemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>())
                .sql("INSERT INTO OphthalmicLens (`createdBy`,`createdDate`,`lastModifiedBy`,`lastModifiedDate`,`sid`,`version`,`manufacturer`,`manufacturerCode`,`name`,`sku`,`upc`,`cylinder`,`design`,`diameter`,`index`,`material`,`source`,`sphere`,`type`) VALUES (:createdBy,NOW(),:lastModifiedBy,NOW(),UUID(),:version,:manufacturer,:manufacturerCode,:name,:sku,:upc,:cylinder,:design,:diameter,:index,:material,:source,:sphere,:type)")
                .dataSource(dataSource).build();
    }

属性设计是一个枚举:

public enum OphthalmicDesign {
    SPHERIC, ASPHERIC, ATORIC, BIASPHERIC
}

在我的生活中 OphthalmicLens 如下所示:

@NotNull
    @Enumerated(EnumType.STRING)
    @Column(nullable = false)
    private OphthalmicDesign design = OphthalmicDesign.SPHERIC;

我使用的是mysql 5.7,数据库中的那一列是mappes,正如预期的那样是varchar(255)。
运行作业时出现以下错误:

Caused by: java.sql.BatchUpdateException: Incorrect string value: '\xAC\xED\x00\x05~r...' for column 'design' at row 1
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_172]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[?:1.8.0_172]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[?:1.8.0_172]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[?:1.8.0_172]
    at com.mysql.cj.util.Util.handleNewInstance(Util.java:210) ~[mysql-connector-java-8.0.11.jar:8.0.11]
    at com.mysql.cj.util.Util.getInstance(Util.java:185) ~[mysql-connector-java-8.0.11.jar:8.0.11]
    at com.mysql.cj.util.Util.getInstance(Util.java:192) ~[mysql-connector-java-8.0.11.jar:8.0.11]
    at com.mysql.cj.jdbc.exceptions.SQLError.createBatchUpdateException(SQLError.java:218) ~[mysql-connector-java-8.0.11.jar:8.0.11]
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeBatchedInserts(ClientPreparedStatement.java:768) ~[mysql-connector-java-8.0.11.jar:8.0.11]
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeBatchInternal(ClientPreparedStatement.java:444) ~[mysql-connector-java-8.0.11.jar:8.0.11]
    at com.mysql.cj.jdbc.StatementImpl.executeBatch(StatementImpl.java:839) ~[mysql-connector-java-8.0.11.jar:8.0.11]
    at com.zaxxer.hikari.pool.ProxyStatement.executeBatch(ProxyStatement.java:128) ~[HikariCP-2.7.9.jar:?]

我知道问题是枚举没有转换成字符串。我可以用不同的方法解决这个问题 BeanPropertyItemSqlParameterSourceProvider 但我认为这样做会过分,我会失去 Spring 设施的优势。
你有没有提示让spring/hibernate在我使用spring批处理时自动将枚举转换成字符串?

kdfy810k

kdfy810k1#

你可以指导 BeanPropertyItemSqlParameterSourceProvider 归还 VARCHAR 字段的类型 design 具体如下:

@Bean
public JdbcBatchItemWriter<OphthalmicLens> writer(DataSource dataSource) throws SQLException {
    return new JdbcBatchItemWriterBuilder<OphthalmicLens>()
            .itemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<OphthalmicLens>() {
                @Override
                public SqlParameterSource createSqlParameterSource(OphthalmicLens item) {
                    return new BeanPropertySqlParameterSource(item) {
                        @Override
                        public int getSqlType(String paramName) {
                            if (paramName.equalsIgnoreCase("desing")) {
                                return Types.VARCHAR;
                            }
                            return super.getSqlType(paramName);
                        }
                    };
                }
            })
            .sql("INSERT INTO OphthalmicLens (`createdBy`,`createdDate`,`lastModifiedBy`,`lastModifiedDate`,`sid`,`version`,`manufacturer`,`manufacturerCode`,`name`,`sku`,`upc`,`cylinder`,`design`,`diameter`,`index`,`material`,`source`,`sphere`,`type`) VALUES (:createdBy,NOW(),:lastModifiedBy,NOW(),UUID(),:version,:manufacturer,:manufacturerCode,:name,:sku,:upc,:cylinder,:design,:diameter,:index,:material,:source,:sphere,:type)")
            .dataSource(dataSource)
            .build();
}

相关问题