Green Dao: Android ORM for your SQLite database

GreenDAO is an open-source Android ORM making development, It is a light & fast ORM for Android that maps objects to SQLite databases. Being highly optimized for Android, greenDAO offers great performance and consumes minimal memory. It relieves developers from dealing with low-level database requirements while saving development time. SQLite is an awesome embedded relational database. Still, writing SQL and parsing query results are quite tedious and time-consuming tasks. greenDAO frees you from these by mapping Java objects to database tables (called ORM, “object/relational mapping”). This way you can store, update, delete, and query for Java objects using a simple object-oriented API.

Features

greenDAO’s unique set of features:

  • Rock-solid: greenDAO has been around since 2011 and is used by countless famous apps
  • Super simple: concise and straight-forward API, in V3 with annotations
  • Small: The library is <150K and it’s just plain Java jar (no CPU dependent native parts)
  • Fast: Probably the fastest ORM for Android, driven by intelligent code generation
  • Safe and expressive query API: QueryBuilder uses property constants to avoid typos
  • Powerful joins: query across entities and even chain joins for complex relations
  • Flexible property types: use custom classes or enums to represent data in your entity
  • Encryption: supports SQLCipher encrypted databases

Add greenDAO to your project

greenDAO is available on Maven Central. Please ensure that you are using the latest versions of the greendao and greendao-gradle-plugin artifact. You can also see the setup here

Add the following Gradle configuration to your Android project. In your root build.gradle file:

buildscript {
repositories {
jcenter()
mavenCentral() // add repository
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
classpath 'org.greenrobot:greendao-gradle-plugin:3.3.0' // add plugin
}
}

In your app modules app/build.gradle file:

apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao' // apply plugin

dependencies {
implementation 'org.greenrobot:greendao:3.3.0' // add library
}

Note that this hooks up the greenDAO Gradle plugin to your build process. When you build your project, it generates classes like DaoMaster, DaoSession, and DAOs.

After adding the GreenDao library dependency and the Gradle plugin, we need to first create an entity object.

Entity

An entity is a Plain Old Java Object (POJO) that models some data in the database. GreenDao will use this class to create a table in the SQLite database and automatically generate helper classes we can use to access and store data without having to write SQL statements.

@Entity
public class Users {
@Id(autoincrement = true)
private Long id;
private String firstname;
private String lastname;
@Unique
private String email;
// Getters and setters for the fields...}

One-time GreenDao setup

Each time an application is launched GreenDao needs to be initialized. GreenDao suggests keeping this code in an Application class or somewhere it will only be run once.

DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "mydatabase", null);
db = helper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(db);
DaoSession daoSession = daoMaster.newSession();

GreenDao Helper Classes

After the entity object is created, GreenDao automatically creates the helper classes used to interact with the database. These are named similarly to the name of the entity object that was created, followed by Dao and are retrieved from the daoSession object.

UsersDao usersDao = daoSession.getUsersDao();

Many typical database actions can now be performed using this Dao object with the entity object.

Query

String email = "jdoe@example.com";
String firstname = "John";
// Single user query WHERE email matches "jdoe@example.com"
Users user = userDao.queryBuilder()
.where(UsersDao.Properties.Email.eq(email)).build().unique();
// Multiple user query WHERE firstname = "John"
List<Users> user = userDao.queryBuilder()
.where(UsersDao.Properties.Firstname.eq(firstname)).build().list();

Insert

Users newUser = new User("John","Doe","jdoe@example.com");
usersDao.insert(newUser);

Update

// Modify a previously retrieved user object and update
user.setLastname("Dole");
usersDao.update(user);

Delete

// Delete a previously retrieved user object
usersDao.delete(user);

R8, ProGuard

If your project uses R8 or ProGuard add the following rules:

-keepclassmembers class * extends org.greenrobot.greendao.AbstractDao {
public static java.lang.String TABLENAME;
}
-keep class **$Properties { *; }
# If you DO use SQLCipher:
-keep class org.greenrobot.greendao.database.SqlCipherEncryptedHelper { *; }
# If you do NOT use SQLCipher:
-dontwarn net.sqlcipher.database.**
# If you do NOT use RxJava:
-dontwarn rx.**

For Complete details of GreenDao please read documentation’s from below:

--

--

--

Senior Software Engineer | Android | Java | Kotlin|Xamarin Native Android|Flutter

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Top 7 most common prejudices about developers

Have you heard of Graph Data Models?

Lambda Custom Authorizer with Auth0 (Serverless Framework + AWS CDK)

Add Two Factor Authentication to Website

Atlassian End of Server — A Review with a View

THE MAJOR DIFFERENCES BETWEEN WEB 2 AND WEB 3

Traveling towards the business outcome!

Decentralized Web Server

Get the Medium app

Abhishek Srivastava

Abhishek Srivastava

Senior Software Engineer | Android | Java | Kotlin|Xamarin Native Android|Flutter

More from Medium

twitter clone || twitter clone app || twitter clone application

Review of OnePlus Nord 2 5G Smart Phone

[android-EN] SSLHandshakeException: Handshake failed

A Comprehensive Guide to React Components and How to Use Them in Your Next Project