【发布时间】:2016-07-13 21:27:36
【问题描述】:
我在 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 注释设置),以便 TINYINT 的 1 值是解析为true?
【问题讨论】: