我正在使用Spring Boot和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
bean中使用的如下:
@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 Batch,您是否有提示告诉Spring / Hibernate自动将该枚举转换为String?