GreenDAO: A fast Android ORM database

When it comes to persisting data on Android, the officially recommended way is to use the SQLite database. Even though the Android framework provides some kind of support in form of the helper classes, the developer still has to struggle with writing raw SQL queries and manually transforming objects to records in the database. This is where Android ORM database libraries come to give a helping hand.

One way of making data persistence on Android with SQLite database easier is to use a technique called ORM (Object-relational mapping). Several Android ORM database libraries exist, simplifying the database-related boilerplate code to a minimum and abstracting the relational database operations for the developer. Let’s have a look at one of their representatives – GreenDAO

GreenDAO – An Android ORM database

GreenDAO is one of the many existing Android ORM database libraries. To start using it in your project, all you have to do is to define your data model. GreenDAO then generates all the DAOs (Data Access Object) and database helper classes automatically for you.

The main advantage of GreenDAO over other Android ORM database libraries, is its high performance. According to their report for year 2016, GreenDAO outperforms all the other compared ORM libraries. Their benchmarks are open-source and freely available to use and review. Among other features GreenDAO provides, there is session caching, eager loading and database encryption to name a few. The size of the library itself is very small (less than 100 kB) which is also a plus.

Recently, a new version of GreenDAO has been released, bringing new features such as annotations for data model classes or an experimental support for JavaRx. Let’s have a look at how to set the library up in your project.

Setting it up

First of all, I recommend keeping the data model in a separate module from your project’s main module. This is because GreenDAO generates the DAO classes of your data models during the compile time. Therefore, having your data models separate allows you to compile data module first and also the generated code doesn’t interfere with your main project code.

The project top-level build.gradle file should contain a dependency for GreenDAO gradle plugin:

The datamodel module’s build.gradle file should look like this, containing the GreenDAO plugin specification and library dependency:

GreenDAO allows to configure the gradle plugin by setting the schema version of the database or the directory into which the DAO classes will be generated. More on the gradle plugin configuration can be found in the official documentation.

Schema modelling

Let’s have a look now on how to create a model class to be used with GreenDAO. In our example, we want to store information about people such as their name, their year of birth and a list of their phone numbers. Therefore, we create two data model classes: Person and PhoneNumber. Each class has to be annotated by the @Entity annotation for GreenDAO to recognize it.

To explain some of the other annotations used:

  • @Id: selects a long/ Long property as the entity ID and a primary key
  • @Property: allows to redefine the default column name for the property (by default it is transformed e.g. from yearOfBirth to YEAR_OF_BIRTH)
  • @NotNull: adds a NOT NULL constrain to the database column (useful for primitive data types)
  • @Transient: marks the property to be excluded from the database (e.g. for temporary data)
  • @Index: create an index on the property

To define relations between tables/model classes, annotations @ToOne and @ToMany can be used. You just need to specify the joinProperty or referencedJoinProperty respectively.

For a comprehensive list of all options in data modelling, please refer to the official documentation.

DB operations

In order to be able to work with the database objects, the datamodel module has to be compiled first. By doing that, all the DAO classes and helper classes get created. As a convenience, we open a database and create a DAO session upon the application start in the onCreate method of custom Application class so that we can access the DaoSession from anywhere in the code:

After that, inserting values into the database is very simple:

We have created two Person objects using the constructors automatically generated by GreenDao, and insert them into the database using appropriate DAO class ( PersonDao). Saving the assigned ID numbers, we create a list of PhoneNumber objects and assign them to people using the saved IDs. For demonstration purposes, the phone numbers are inserted into the database in a transaction.

The dump of the database shows what operations have been processed:

Querying the database is made simple with the QueryBuilder. An example shows getting all objects of a certain type from the database and creating a query using join:

A few more examples of database operations made easy with GreenDAO follow:

After the operations are executed we can confirm that the information about the person “Jack” have been removed together with phone numbers associated with him. Also, the surname of “James” has been successfully updated:

Conclusion

GreenDAO is one of the many Android ORM database libraries available. What makes it stand out is its performance and advanced caching support. It’s being actively developed and new features are added with each new release.

In this post, it has been described how to set up a project with GreenDAO, how to create data model classes and several common database operations have been presented.

Sources

Share this:

Leave a Comment