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();
}
The property design is an enumeration:
public enum OphthalmicDesign {
SPHERIC, ASPHERIC, ATORIC, BIASPHERIC
}
that is used in my OphthalmicLens
beans as this:
@NotNull
@Enumerated(EnumType.STRING)
@Column(nullable = false)
private OphthalmicDesign design = OphthalmicDesign.SPHERIC;
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:?]
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.
Do you have an hint to tell Spring/Hibernate to automatically convert that enum to a String even when I use Spring Batch?
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();
}