Introduction


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:

Then model your entities and make your project, for example using “Build > Make Project” in Android Studio.

Core Classes

Once your project is built, 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).

Core Initialization

Finally, the following code example illustrates the first steps to initialize the database and the core greenDAO classes:

The example assumes a Note entity exists. With its DAO ( noteDao object), we can call the persistence operation for this specific entity.

Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •