可以让 OrmLite 的 dao.create 向 int 字段插入一个空值而不是 0 吗?

Can one make OrmLite's dao.create to insert a null value to an int field instead of 0?

是否可以让 dao.create()null 插入默认的空整数字段而不是 0?

根据文档,@DatabaseField 注释的 canBeNull 参数默认为 true,我还没有找到如何将 null 值作为 defaultValue 参数。

Is it possible to make dao.create() insert null into a default null integer field instead of 0?

是的,你可以做到。

I'm yet to find a way how to feed null - value as a defaultValue - argument.

默认情况下,应为任何未初始化的字段分配数据库中该类型的默认值。例如,创建实体时未设置值的 int 字段在数据库中的值应为 0。对于任何 object 字段(例如 Integer),默认值已经是 null。您需要做的就是 而不是 提供 defaultValue.

例如,以下代码对我有用:

public class Foo {
     @DatabaseField(generatedId = true)
     private int id;
     @DatabaseField // no default-value specified
     private Integer someIntegerField;
}
...
Foo foo = new Foo();
// leave someIntegerField as a null value without a default-value defined
assertEquals(1, dao.create(foo));

Foo result = dao.queryForId(foo.id);
// by default the field that we did not initialize comes back as null
assertNull(result.someIntegerField);

如果您提供默认值,那么 ORMLite 会尝试将其转换为整数值,以便对其进行分配。只需删除默认值设置即可。