Contents
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:
1 2 3 4 | // In your (app) module build.gradle file: dependencies { compile "org.greenrobot:objectbox-daocompat:<version>" } |
Get the latest version on Maven Central.
Then, tell the ObjectBox annotation processor to enable DaoCompat mode:
For Java-only Android projects
1 2 3 4 5 6 7 8 9 | android { defaultConfig { javaCompileOptions { annotationProcessorOptions { arguments = [ 'objectbox.daoCompat' : 'true' ] } } } } |
For Kotlin projects
1 2 3 4 5 | kapt { arguments { arg("objectbox.daoCompat", true) } } |
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.
1 2 3 4 5 6 7 | // From the greenDao package name... import org.greenrobot.greendao.annotation.Entity; import org.greenrobot.greendao.annotation... // ...to the ObjectBox package name import io.objectbox.annotation.Entity; import io.objectbox.annotation... |
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.
1 2 3 4 | // greenDAO @Id private Long id; // ObjectBox @Id private long id; |
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:
1 2 3 4 5 6 7 8 9 | // greenDAO @Entity(indexes = ...) @Index(name = "idx1", unique = true) private String name; @Unique private String name; // ObjectBox @Index private String name; |
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.
1 2 3 4 | // greenDAO import org.greenrobot.greendao.converter.PropertyConverter; // ObjectBox import io.objectbox.converter.PropertyConverter; |
Rename the columnType property on @Convert annotations to dbType:
1 2 3 4 | // greenDAO @Convert(converter = NoteTypeConverter.class, columnType = String.class) // ObjectBox @Convert(converter = NoteTypeConverter.class, dbType = String.class) |
Relations
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.
1 2 | boxStore = MyObjectBox.builder().androidContext(this).build(); daoCompatSession = new DaoSession(boxStore); |
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:
1 2 3 4 | // note example List<com.example.app.daos.greendao.Note> notes = daoSession.getNoteDao().loadAll(); List<Note> convertedNotes = convertToObjectBoxNotes(notes); daoCompatSession.getNoteDao().insertInTx(convertedNotes); |
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:
1 2 3 4 5 6 | public DaoSession getDaoSession() { // greenDAO // return daoSession; // ObjectBox return daoCompatSession; } |
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:
1 2 3 4 5 6 | // greenDAO import org.greenrobot.greendao.query.Query; import org.greenrobot.greendao... // ObjectBox import org.greenrobot.daocompat.query.Query; import org.greenrobot.daocompat... |
Queries
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.