尽管setPrimitivesDefaultedForNullValue(true),BeanPropertyRowMapper抛出org.springframework.beans.TypeMismatchException

问题描述 投票:1回答:1

在我的Spring Batch应用程序中,我读取了一个数据库表并将其映射到dto。我的dto有一些类型boolean的领域。

我的Dto和dao方法如下所示:

public class MyDto {

    private long id;
    private String yyy;
    private boolean xxx;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getYyy() {
        return id;
    }

    public void setYyy(String yyy) {
        this.yyy = yyy;
    }

    public boolean getXxx() {
        return xxx;
    }

    public void setXxx(boolean xxx) {
        this.xxx = xxx;
    }
}

@Override
public MyDto readMyDto(String id) {  
    SqlParameterSource paramMap = new MapSqlParameterSource(ID, id);
    BeanPropertyRowMapper<MyDto > rowMapper = new BeanPropertyRowMapper<>(MyDto.class);
    rowMapper.setPrimitivesDefaultedForNullValue(true);
    try {
        return this.jdbcTemplateStpl.queryForObject(LESE_KRAFTST_STEUERFALL, paramMap, rowMapper);
    } catch (EmptyResultDataAccessException e) {
        return null;
    }
}      

虽然我把Caused by: java.lang.IllegalArgumentException: Cannot convert value of type [null] to required type [boolean] for property 'xxx'设置为setPrimitivesDefaultedForNullValue,但我得到了true

在我的UnitTests中它工作正常,即使有例外。当我将setPrimitivesDefaultedForNullValue设置为false时,测试将会像Caused by: java.lang.IllegalArgumentException: Cannot convert value of type [null] to required type [boolean] for property 'xxx'.一样失败

这个例外是setPrimitivesDefaultedForNullValue设置为truean错误在我这边或Spring Batch,或者这是预期的行为和BeanPropertyRowMapper捕获异常并在这种情况下默认为null(布尔值)?

java spring-batch
1个回答
1
投票
Your method..  
public void setId(boolean xxx) {
        this.xxx = xxx;
    }

should be
public void setXxx(boolean xxx) {
        this.xxx = xxx;
    }
© www.soinside.com 2019 - 2024. All rights reserved.