Spring批量导入期间将枚举转换为字符串失败 [英] Enum to String conversion during Spring batch import fails

查看:58
本文介绍了Spring批量导入期间将枚举转换为字符串失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring Boot和Hiberna创建Spring批处理作业,但是在插入过程中遇到了问题.这是代码的相关部分:

I'm creating a Spring batch job using Spring Boot and Hiberna but I'm experiencing a problem during the insert. This is the relevant part of the code:

@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 bean中使用的

如下:

that is used in my OphthalmicLens beans as this:

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

我正在使用Mysql 5.7,并且数据库中的该列按预期的那样是mappes作为VARCHAR(255).

I'm using Mysql 5.7 and that column in the database is mappes as a VARCHAR(255) as expected.

当我运行作业时,出现此错误:

When I run the job I get this error:

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设施的优势.

I understand the problem is the enum is not converted to a String. I could solve the problem using a different BeanPropertyItemSqlParameterSourceProvider but I think that would be overkilled and I would loose advantage of Spring facilities.

即使我使用Spring Batch,您是否有提示告诉Spring/Hibernate自动将该枚举转换为字符串?

Do you have an hint to tell Spring/Hibernate to automatically convert that enum to a String even when I use Spring Batch?

推荐答案

您可以指示 BeanPropertyItemSqlParameterSourceProvider design字段返回 VARCHAR 类型.代码>,如下所示:

You can instruct the BeanPropertyItemSqlParameterSourceProvider to return the VARCHAR type for the field design as follows:

@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();
}

这篇关于Spring批量导入期间将枚举转换为字符串失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆