繁体   English   中英

如何使用ORMLite原始查询获取正确的布尔字段值?

[英]How do I fetch correct boolean field values with an ORMLite raw query?

我在Java中使用带有H2数据库的ORMLite,并且我有一个带有布尔字段的类。 当我使用原始查询和DAO的默认原始行映射器从数据库中获取此类的对象时,返回的对象中的布尔字段的值始终为false。 (这些值在数据库中以TINYINT类型存储。)

这是一个例子:

public class BooleanPersistenceWithRawQueries {

    @DatabaseTable
    public static class George {
        @DatabaseField(generatedId = true) public Integer id;
        @DatabaseField public boolean curious;
    }

    public static void main(String[] args) throws Exception {
        ConnectionSource connectionSource = new JdbcConnectionSource("jdbc:h2:mem:");
        Dao<George, ?> dao = DaoManager.createDao(connectionSource, George.class);
        TableUtils.createTable(connectionSource, George.class);
        George g = new George();
        g.curious = true;
        dao.create(g);
        George h = dao.queryRaw("SELECT * FROM George", dao.getRawRowMapper()).getFirstResult();
        System.out.println("curious = " + h.curious + " should be " + g.curious);
    }
}

输出是

curious = false should be true

我知道我可以将RawRowMapperImpl子类RawRowMapperImpl覆盖此行为,但是是否有一种内置方法来配置对象映射(例如@DatabaseField注释设置),以便将TINYINT1解析为true

这是一些古老的问题,但可能会对某些问题有所帮助。 在表示法参数中使用数据类型

  @DatabaseField(dataType = DataType.BOOLEAN)

我有同样的问题并且由于Kamila解决了,但在这种情况下他的答案是不正确的。 你必须写:

@DatabaseField(dataType=DataType.BOOLEAN_INTEGER)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM