Hibernate Converter导致SQLException

时间:2015-08-31 21:50:35

标签: java hibernate entity converter

在我的Hibernate实体类中,我有一个布尔值,我想对VARCHAR2(1)列中的某些字符使用。但是我的代码会导致SQLException: Fail to convert to internal representation。有人能看出我做错了什么吗?我的代码如下:

在我的实体中,我有:

...

    private Boolean flag;

    @Column(length = 1, name = "FLAG")
    @Convert(converter = FlagToBooleanConverter.class)
    public Boolean getFlag()
    {
        return flag;
    }

    void setFlag(Boolean flag)
    {
        this.flag = flag;
    }

...

@Converter
class FlagToBooleanConverter implements AttributeConverter<Boolean, String>
{
    @Override
    public Boolean convertToEntityAttribute(String value)
    {
        return (value.equals("X") || value.equals("Y"));
    }

    @Override
    public Boolean convertToDatabaseColumn(String value)
    {
        // My class should only read from the DB so I don't need this but I have to override it
        return null;
    }
}

更新:感觉有点hacky,但此刻,为了让工作失败,我一直在使用:

    private String flag;

    @Column(length = 1, name = "FLAG")
    String getFlag()
    {
        return flag;
    }

    void setFlag(String flag)
    {
        this.flag = flag;
    }

    @Transient
    public Boolean isFlag()
    {
        return (value.equals("X") || value.equals("Y"));
    }

0 个答案:

没有答案