ObjectBox Data Model Updates

ObjectBox manages its data model (schema) mostly automatically. The data model is defined by the entity classes you define. When you add or remove entities or properties of your entities, ObjectBox takes care of those changes without any further action from you.

For other changes like renaming or changing the type, ObjectBox needs extra information to make things unambiguous. This works using unique IDs (UIDs) and an @Uid annotation, as we will see below.

Note: ObjectBox v1.0.1 or later is required for this to work.

UIDs

ObjectBox keeps track of entities and properties by assigning them unique IDs (UIDs). All those UIDs are stored in a file “objectbox-models/default.json”, which you should add to your version control system (e.g. git). If you are interested, we have an in-depth documentation on UIDs and concepts. But let’s continue with how to rename entities or properties.

In short: To make UID-related changes, put an  @Uid annotation on the entity or property and build the project to get further instructions.

Renaming Entities and Properties

So why do we need that UID annotation? If you simply rename an entity class, ObjectBox only sees that the old entity is gone and a new entity is available. This can be interpreted in two ways:

  • The old entity is removed and a new entity should be added, the old data is discarded. This is the default behavior of ObjectBox.
  • The entity was renamed, the old data should be re-used.

So to tell ObjectBox to do a rename instead of discarding your old entity and data, you need to make sure it knows that this is the same entity and not a new one. You do that by attaching the internal UID to the entity.

The same is true for properties.

Now let’s walk through how to do that. The process works the same if you want to rename a property:

How-to and Example

Step 1: Add an empty @Uid  annotation to the entity/property you want to rename:

Step 2: Build the project. The build will fail with an error message that gives you the current UID of the entity/property:

Step 3: Apply the UID from the [Rename] section of the error message to your entity/property:

Step 4: The last thing to do is the actual rename on the language level (Java, Kotlin, etc.):

You can now use your renamed entity/property as expected and all existing data will still be there.

Note: Instead of the above you can also find the UID of the entity/property in the ObjectBox default.json file yourself and add it together with the @Uid annotation before renaming your entity/property.

Changing Property Types

When you want to change the type of a property, you must tell ObjectBox to create a new property internally. This is because ObjectBox cannot migrate your data, so simply changing the type may lead to data loss. You can do this in two ways:

  • Just rename the property, so it is treated as a new property (this only works if the property has no @Uid annotation already):

  • Tell ObjectBox to use a new UID for the property. Let’s walk through how to do that:

How-to and Example

Step 1: Add the  @Uid annotation to the property where you want to change the type:

Step 2: Build the project. The build will fail with an error message that gives you a newly created UID value:

Step 3: Apply the UID from the [Change/reset] section to your property:

You can now use the property in your entity as if it were a new one.

 

Spread the love
Sign up for fresh ObjectBOX news here. No spam, just fresh developer news once in a while.
x