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

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

我在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来覆盖这种行为,但有没有一种内置的方法来配置对象映射(例如@DatabaseField注释设置),以便TINYINT1值被解析为true

java ormlite
2个回答
0
投票

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

  @DatabaseField(dataType = DataType.BOOLEAN)

0
投票

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

@DatabaseField(dataType=DataType.BOOLEAN_INTEGER)
© www.soinside.com 2019 - 2024. All rights reserved.