我在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
?
这是一些古老的问题,但可能会对某些问题有所帮助。在表示法参数中使用数据类型
@DatabaseField(dataType = DataType.BOOLEAN)
我有同样的问题并且由于Kamila解决了,但在这种情况下他的答案是不正确的。你必须写:
@DatabaseField(dataType=DataType.BOOLEAN_INTEGER)