Tallan Blog

Tallan’s Experts Share Their Knowledge on Technology, Trends and Solutions to Business Challenges

Android Database Modeling Made Easy with GreenDAO ORM

Recently I attempted to create an application that worked in an “Offline” mode.  However one of the biggest hurdles I encountered was maintaining the data model throughout development.  As most projects go, the definition of the database tables changed frequently, and maintaining the structure became a tangled mess of version numbers, alter statements and data migration scripts.  In trying to solve for this problem and adhere to good practices, I created classes that stored my column names and indexes so I could later read them back from the cursor.  Trying to add columns, though proved to be very code intensive.

After scrapping the idea due to a tight time line, I began researching other solutions in the JAVA/Android community.  I came across GreenDAO ORM (http://greendao-orm.com/).  It is a complete code generation which allows you to generate your data model without having to worry about creating each table and managing the column names and indexes.  The site has extremely comprehensive documentation from Getting Started to Creating Your Model to advance topics like Model Relationships and Querying Entities.

If you have used the SQLite API directly to create a table and manage that schema the following should look familiar, if not induce nightmares.

	public void onCreate(SQLiteDatabase db) {
		Log.d(TAG, "onCreate: Entry");
		String CREATE_CLIENT_TABLE = "CREATE TABLE " + ClientTableHelper.TABLE_NAME + " (" +
				ClientTableHelper.KEY_CLIENTID + " INTEGER PRIMARY KEY, " +
				ClientTableHelper.KEY_PREFIX + " TEXT, " +
				ClientTableHelper.KEY_FIRSTNAME + " TEXT, " +
				ClientTableHelper.KEY_MIDDLENAME + " TEXT, " +
				ClientTableHelper.KEY_LASTNAME + " TEXT, " +
				ClientTableHelper.KEY_EMAILADDRESS + " TEXT, " +
				ClientTableHelper.KEY_WORKNUMBER + " TEXT, " +
				ClientTableHelper.KEY_WORKEXTENSION + " TEXT, " +
				ClientTableHelper.KEY_MOBILENUMBER + " TEXT, " +
				ClientTableHelper.KEY_FAXNUMBER + " TEXT, " +
				ClientTableHelper.KEY_NICKNAME + " TEXT, " +
				ClientTableHelper.KEY_IMAGEPATH + " TEXT, " +
				ClientTableHelper.KEY_IMAGEFILENAME + " TEXT, " +
				ClientTableHelper.KEY_NOTES + " TEXT, " +
				ClientTableHelper.KEY_OFFLINEIMAGEPATH + " TEXT, " +
				ClientTableHelper.KEY_UPDATEDATE + " LONG, " +
				ClientTableHelper.KEY_ISNEW + " TEXT)";

The above snippet is just to create the database table. It does not include any JAVA objects to make that information available in your code. Also, the ClientTableHelper referenced is a class that was needed to store both column names and column indexes. So any edits to the schema required you to change the database helper class, the table helper class, then make edits to your data objects.

The same code using GreenDAO ORM looks more like this.

The first step is to edit your “Generator” class.

    public static void main(String[] args) throws Exception {
        Schema schema = new Schema(1, "com.tallan.android.testApp.gDao");



        new DaoGenerator().generateAll(schema, "../testApp/src-gen");

    private static void addClient(Schema schema) {
    	client = schema.addEntity("client");



After running the “Generator” project you will have your data access objects, with appropriate referential links between them. The generation process handles managing the schema creation so you can query the data, and handles creating your data objects for use in code.

NOTE: Regeneration of your data access objects will cause GreenDAO to destroy the database on the device and rebuild the schema. For the most part this should be acceptable as your mobile device is rarely a system of record, but if you need to migrate data you would have to manually code the onUpgrade method of the generated SQLiteOpenHelper to modify your schema if the version has changed.

In my opinion, writing this JAVA code to generate a scheme is much easier than trying to manage the overhead of column names and indexes.

For a complete walkthrough of GreenDAO ORM visit http://greendao-orm.com/documentation/ and run through their very thorough guide.  Also check out some of the users of GreenDAO here.Driec

Learn more about Tallan or see us in person at one of our many Events!

Share this post:

2 Comments. Leave new

I’m always get
Error:Gradle: Execution failed for task ‘:app:dexDebug’.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process ‘command ‘C:\Program Files\Java\jdk1.8.0_45\bin\java.exe” finished with non-zero exit value 2
when i run Generator java class

Nothing just go in android studio go to build=>clear project.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>