greenDAO is an object/relational mapping (ORM) tool for Android. It offers an object oriented interface to the relational database SQLite. ORM tools like greenDAO do many repetitive tasks for you and offer a simple interface to your data.
Gradle plugin and DAO code generation
In order to use greenDAO in your Android project, you need to add the greenDAO Gradle plugin and add the greenDAO library:
// In your root build.gradle file:
// In your app projects build.gradle file:
apply plugin: 'com.android.application'
Then model your entities and make your project, for example using Build > Make Project in Android Studio.
Once you have defined at least one entity and built your project, you can start using greenDAO in your Android project.
The following core classes are the essential interface to greenDAO:
DaoMaster: The entry point for using greenDAO. DaoMaster holds the database object (SQLiteDatabase) and manages DAO classes (not objects) for a specific schema. It has static methods to create the tables or drop them. Its inner classes OpenHelper and DevOpenHelper are SQLiteOpenHelper implementations that create the schema in the SQLite database.
DaoSession: Manages all available DAO objects for a specific schema, which you can acquire using one of the getter methods. DaoSession provides also some generic persistence methods like insert, load, update, refresh and delete for entities. Lastly, a DaoSession objects also keeps track of an identity scope. For more details, have a look at the session documentation.
DAOs: Data access objects (DAOs) persists and queries for entities. For each entity, greenDAO generates a DAO. It has more persistence methods than DaoSession, for example: count, loadAll, and insertInTx.
Entities: Persistable objects. Usually, entities are objects representing a database row using standard Java properties (like a POJO or a JavaBean).
Finally, the first steps to initialize the database and the core greenDAO classes:
// do this once, for example in your Application class
helper = new DaoMaster.DevOpenHelper(this, "notes-db", null);
db = helper.getWritableDatabase();
daoMaster = new DaoMaster(db);
daoSession = daoMaster.newSession();
// do this in your activities/fragments to get hold of a DAO
noteDao = daoSession.getNoteDao();
The example assumes a Note entity exists. With its DAO ( noteDao object), we can call the persistence operation for this specific entity.