ObjectBox DaoCompat


DaoCompat is meant to help you switch from greenDAO to ObjectBox faster: It allows you to re-use most of your existing code for greenDAO, like DAOs and queries, but using ObjectBox instead of a SQLite database. This page will also go into details how DaoCompat/ObjectBox is different from greenDAO/SQLite.

Should I switch to ObjectBox?

Migrating from greenDAO to ObjectBox offers various advantages:

  • Much faster: ObjectBox is up to 10 times faster than SQLite (check this open source benchmark app)
  • Strong support for relations: ObjectBox offers change tracking, cascading puts, and flexible loading strategies (eager and lazy)
  • No need to master SQL: ObjectBox is easier to use without the need to learn a “foreign” language
  • Modern APIs: ObjectBox has simplified APIs and comes with its own reactive queries and support for RxJava 2/3 (with real change observing)
  • Cleaner entity code: ObjectBox has invisible code generation and does not generate sources into your files
  • Kotlin support: ObjectBox supports Kotlin including data classes
  • Sync coming soon: Keeping data in sync is hard to get right based on typical REST networking approaches. ObjectBox Sync makes that simple.

ObjectBox works in many ways like greenDAO (e.g. entities are POJOs without threading restrictions) and using DaoCompat we migrated apps quite quickly.

When not to switch?

If you are a SQL fan you should stick to a SQLite based solution like greenDAO or Room. Similarly, if app relies heavily on running SQL, database views, or SQLite specific features, be aware of extra migration efforts. After all, ObjectBox is a new database built around objects and not SQL.

Add ObjectBox dependencies

Start by setting up ObjectBox as described in the introduction.

Afterwards, add the DaoCompat library:

Get the latest version on Maven Central.

Then, tell the ObjectBox annotation processor to enable DaoCompat mode:

For Java-only Android projects

For Kotlin projects

Update entity classes

Note: If you are planning on migrating data from your greenDAO database to ObjectBox, you might want to keep your greenDAO entity classes: copy them to a different package, then modify the original versions as described below. This will allow greenDAO to keep generating your DAO classes and you to access data through greenDAO for migration purposes. You may then remove the legacy classes package and greenDAO in a future version of your app.

Continue by changing the imports of your entity annotations: use ObjectBox annotations instead. Note that not all annotations of greenDAO are supported in ObjectBox. See below for details about what is supported.

If they are not already, change IDs to be of type long (instead of Long). Note that there are differences to how IDs work in ObjectBox.

Property indexes

ObjectBox does currently not support unique indexes, naming indexes (as it does not use a SQLite database) or indexes over multiple properties. There is just a simple property index:

Custom types

If you are using custom types, change the imports of your custom property converters. Note: if you want to migrate your data to ObjectBox as described above, you can instead make your converters implement both interfaces to continue to use them in your greenDAO entities.

Rename the columnType property on @Convert annotations to dbType:


Different from the greenDAO @ToOne and @ToMany annotations, ObjectBox uses ToOne  and ToMany  types. You will have to changes your entities to use them instead of the greenDAO annotations. You can learn more in the Relations documentation.

Use BoxStore

After finishing changes to your entities, set up BoxStore to create a DaoCompat DaoSession. On Android you would typically do this in your Application class.

If you want to migrate your existing greenDAO data to ObjectBox, as described above, you may also kick-off a migration task afterwards. Inside the task set up a greenDAO session to get your entities, convert them to ObjectBox entities and insert them using the DaoCompat session:

Note: by default, if no ID is set (id == 0), ObjectBox assigns a new ID to the inserted entities. If you want to keep the IDs of your entities, you may want to change the @Id annotation for those entities to @Id(assignable = true).

Use DaoCompat DaoSession

After setting up the compatibility session, replace all uses of your greenDAO DaoSession with the new compat session. In this example we get the current  DaoSession through a method in the Application class. So we just change the object returned by that getter:

The compat DaoSession mirrors the methods of the greenDAO DaoSession. However, in the background it uses your BoxStore instead of the greenDAO database. So without further changes most of your greenDAO code should now work with ObjectBox.

If you are using additional greenDAO features, like queries, you have to change some additional imports:


DaoCompat only supports a simple  Query, but with additional functionality:

  • Instead of a DeleteQuery you can call remove() on a compat query.
  • Instead of a CountQuery you can call count() on a compat query.
  • CursorQuery is not supported.

You should now be able to Build > Make Project in Android Studio without any errors.

Remove greenDAO dependencies

Remove the greenDAO plugin and library from your root and app build.gradle files. If you do need them to migrate data to ObjectBox, you can also do this at some later point.

Differences to greenDAO

In addition to the changes mentioned above, here are some notable differences between greenDAO and DaoCompat for ObjectBox:

  • The NotNull annotation is not supported. As ObjectBox is based on objects, use the Android support annotations to ensure value initialization.
  • Joins and raw SQL queries are not supported, as ObjectBox is not based on a SQL database. Hence, compat DAOs and DaoSession are also missing methods related to SQLite ( getDatabase() ,  getAllColumns() ,  loadByRowId(), …).
  • DaoCompat does not support async sessions ( startAsyncSession()).
  • Encryption is not supported.
  • DaoCompat only provides simple AbstractDaoTest and AbstractDaoBasicTest classes which require additional setup (compared to AbstractDaoTestLongPk and others provided by greenDAO).

Keep in mind that DaoCompat is meant as a stop-gap solution. You should use the ObjectBox BoxStore and Box API to add, modify or query for objects in any new code to get access to all the features ObjectBox has to offer.

Spread the love