Is it possible to make dao.create()
insert null
into a default null integer field instead of 0?
The canBeNull
argument of the @DatabaseField
annotation defaults to true anyway according to the docs and I'm yet to find a way how to feed null
value as a defaultValue
argument.
Is it possible to make dao.create() insert null into a default null integer field instead of 0?
Yes, you can do that.
I'm yet to find a way how to feed null - value as a defaultValue - argument.
By default, any uninitialized field should be assigned that type's default value in the database. For example, an int
field with no value set on it when the entity is created should get a value of 0 in the database. With any object field (such as Integer
) the default value is already null
. All you need to do is to not provide a defaultValue
.
For example the following code works for me:
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);
If you provide a default-value then ORMLite is going to try to convert it to an integer value so it can assign it. Just remove the default-value setting and it should work.