You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Im using ObjectBox in a pet android project written in Kotlin.. I have a data class encoded as such (example):
@Entity data classFoo(varname:String, vardescription:String) {
constructor() :this("", "") // parameter-less constructor
@Id var id:Long=0L// other stuff
}
Database already existed on the device.. I added an additional property to the Foo class (not in the data constructor):
@Entity data classFoo(varname:String, vardescription:String) {
constructor() :this("", "") // parameter-less constructor
@Id var id:Long=0Lvar bar:String=""// other stuff
}
defaulted, non-null object bar.. however when pulling existing records from the stored box, that value bar appears to be null.. I would expect it to be a blank string (default for the parameter); is the violation of not-null guarantee coming from the Java interop?
I fixed the issue by nuking the database; afterwords the entity created and stored is not-null.. But this brings up any possible future updates that may violate this.
Is there a "database upgrade" method for updating already stored objects between versions, adding default values to new properties, or am I resigned to make anything added to an existing model be nullable (and handle it as such)?
It's quite easy to trick the Kotlin non-nullable types, e.g. with JSON parsers. Same is true for ObjectBox. We will target those kind of oddities in a future version. For now, we suggest to either use nullable types, or have some update code in place. Recipe for the latter:
Have a flag (via SharedPrefs for example) like "fooBarChecked"
If flag is false, run a migration and set the flag
Migration: run a query to get all Foos with bar == null and update the resulting list
With 2.6.0-RC there is very initial support for a @DefaultValue annotation. Currently only String properties are supported, and only the empty string "" as default value.
Usage:
@DefaultValue("")
StringexampleProperty;
@DefaultValue("")
var exampleProperty:String=""
Edit: This annotation currently simply adds the new built-in NullToEmptyStringConverter to the property:
@greenrobot-team To pick up on the annotation, maybe this would be better?
@DefaultForNull(factory = MyValueProvider.class)
Where MyValueProvider would implement an interface "ValueProvider" which basically has a single method like Object provideValue(). The advantage would be that it could provide complex types like lists, arrays and flex. Disadvantage: another thing passed down to JNI, and not clear if wrappers like Integer demand extra effort for primitive types like int.
Activity
greenrobot commentedon Aug 23, 2017
It's quite easy to trick the Kotlin non-nullable types, e.g. with JSON parsers. Same is true for ObjectBox. We will target those kind of oddities in a future version. For now, we suggest to either use nullable types, or have some update code in place. Recipe for the latter:
greenrobot commentedon Aug 25, 2017
Maybe an annotation for defining "defaults" for null values? E.g.
greenrobot-team commentedon Apr 17, 2018
No response. Closing. -ut
greenrobot-team commentedon Feb 26, 2019
Reopening to discuss default values (in Java, but also Kotlin classes with default values).
This also affects queries (merged from #589), e.g. if adding
intProperty, then querying box with existing entities:-ut
[-]Stored object "upgrade", defaulted property in Kotlin[/-][+]Default values for new properties[/+]greenrobot-team commentedon May 4, 2020
With
2.6.0-RCthere is very initial support for a@DefaultValueannotation. Currently only String properties are supported, and only the empty string""as default value.Usage:
Edit: This annotation currently simply adds the new built-in NullToEmptyStringConverter to the property:
objectbox-java/objectbox-java/src/main/java/io/objectbox/converter/NullToEmptyStringConverter.java
Lines 8 to 22 in 220fc35
So to customize default values, create your own converter and replace
@DefaultValuewith@Convert.greenrobot commentedon May 4, 2020
My first assessment is that we should keep default values separate from queries. E.g. if you want to, you can query for null values.
pratikbutani commentedon Aug 5, 2020
What about the default values of booleans? I had taken a boolean variable. It's giving me the default value "false" but I want true then?
greenrobot commentedon Aug 5, 2020
@pratikbutani The default of booleans is false. Are you saying you experienced something else?
11 remaining items
neroux commentedon Apr 18, 2023
neroux commentedon Apr 18, 2023
neroux commentedon Apr 24, 2023
greenrobot commentedon Apr 24, 2023
neroux commentedon Apr 24, 2023
greenrobot commentedon Apr 24, 2023
neroux commentedon Apr 24, 2023
greenrobot commentedon Apr 24, 2023
neroux commentedon Apr 24, 2023
greenrobot commentedon Apr 24, 2023
neroux commentedon Apr 24, 2023
greenrobot commentedon Apr 24, 2023
@greenrobot-team To pick up on the annotation, maybe this would be better?
@DefaultForNull(factory = MyValueProvider.class)Where MyValueProvider would implement an interface "ValueProvider" which basically has a single method like
Object provideValue(). The advantage would be that it could provide complex types like lists, arrays and flex. Disadvantage: another thing passed down to JNI, and not clear if wrappers likeIntegerdemand extra effort for primitive types likeint.RobbWatershed commentedon Sep 13, 2026
greenrobot-team commentedon Sep 14, 2026
RobbWatershed commentedon Sep 18, 2026