JdbcBatchItemWriter-setSql-如何修复时间戳类型不匹配

时间:2019-07-17 18:22:19

标签: spring-boot spring-jdbc

我在Springboot应用程序中使用JdbcBatchItemWriter将数据从CSV文件导入到Oracle DB中,但无法导入时间戳。我收到错误Failed to convert property value of type 'java.lang.String' to required type 'java.time.LocalDateTime 我了解错误-我只是不知道如何解决。有没有办法标记插入语句,以便将其作为时间戳读取?

我确实尝试过: INSERT INTO HB357(START_DATE) VALUES (TO_DATE(:START_DATE, YYYY-MM-DD, HH:mm:SS) 我试过了 TO_TIMESTAMP(:START_DATE, YYYY-MM-DD HH24:mm:SS) 但这没用

我还尝试将实体类型更改为String而不是LocalDataTime,但这会导致not a valid month错误。 因此,我然后尝试在插入之前更改会话(实体类型仍为字符串): itemWriter.setSql("ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS' NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS'"); 然后运行插入状态,但也出现not a valid month错误

ENTITY:

@Entity
@NamedQuery(name ="HB357.getListOfResponseIds", query = "SELECT RESPONSE_ID from HB357")
public class HB357 {

    @Id
    private String RESPONSE_ID;
    @JsonFormat(pattern="yyyy-MM-dd@HH:mm:ss")
    private LocalDateTime START_DATE;
    private String STOVE_OPTION;
    private String FIRST_NAME;
    private String LAST_NAME;
    private String ADDRESS;
    private String CITY;
    private String ZIPCODE;
    private String COUNTY;

//Getters and Setters left out for brevity

相关插入功能

@Bean
    public JdbcBatchItemWriter<HB357> writer() {
        JdbcBatchItemWriter<HB357> itemWriter = new JdbcBatchItemWriter<>();
        itemWriter.setDataSource(appProperties.dataSource());
        itemWriter.setSql("INSERT INTO HB357 (" +
                "START_DATE, RESPONSE_ID, STOVE_OPTION, FIRST_NAME,LAST_NAME,ADDRESS,CITY,ZIPCODE,COUNTY,PHONE_NUMBER,EMAIL," +
                "STOVE_LOCATION,EXEMPTIONS,AGI,INCENTIVE,INCENTIVE_AMT) " +
                "VALUES (:START_DATE, :RESPONSE_ID, :STOVE_OPTION, :FIRST_NAME, :LAST_NAME, :ADDRESS, :CITY," +
                ":ZIPCODE, :COUNTY, :PHONE_NUMBER, :EMAIL, :STOVE_LOCATION, :EXEMPTIONS, :AGI," +
                ":INCENTIVE, :INCENTIVE_AMT)" );
        itemWriter.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>());
        return itemWriter;
    }

0 个答案:

没有答案