greenDAO
greenDAO is a light & fast ORM solution for Android that maps objects to SQLite databases.
Top Related Projects
Android Database - first and fast, lightweight on-device vector database
Realm is a mobile database: a replacement for SQLite & ORMs
requery - modern SQL based query & persistence for Java / Kotlin / Android
Insanely easy way to work with Android Database.
A blazing fast, powerful, and very simple ORM android database library that writes database code for you.
ORMLite Android functionality used in conjunction with ormlite-core
Quick Overview
greenDAO is an open-source ORM (Object-Relational Mapping) library for Android SQLite databases. It simplifies database operations by allowing developers to work with Java objects instead of raw SQL queries, making data persistence more efficient and less error-prone.
Pros
- High performance due to its optimized database operations
- Easy to use with a simple API and code generation
- Lightweight and has a small footprint, suitable for Android development
- Supports database encryption for enhanced security
Cons
- Limited support for complex queries and relationships compared to some other ORM solutions
- Learning curve for developers new to ORM concepts
- Less flexibility in fine-tuning database operations compared to raw SQL
- Not actively maintained, with the last major update in 2019
Code Examples
- Defining an entity:
@Entity
public class User {
@Id
private Long id;
private String name;
private int age;
// Getters and setters
}
- Inserting a new entity:
User user = new User();
user.setName("John Doe");
user.setAge(30);
daoSession.getUserDao().insert(user);
- Querying entities:
List<User> users = daoSession.getUserDao().queryBuilder()
.where(UserDao.Properties.Age.gt(18))
.orderAsc(UserDao.Properties.Name)
.list();
Getting Started
- Add greenDAO to your project's
build.gradle
:
dependencies {
implementation 'org.greenrobot:greendao:3.3.0'
}
- Configure the greenDAO plugin in your app's
build.gradle
:
apply plugin: 'org.greenrobot.greendao'
greendao {
schemaVersion 1
}
-
Create your entity classes with appropriate annotations.
-
Build your project to generate the DAO classes.
-
Initialize greenDAO in your application class:
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "my-db");
Database db = helper.getWritableDb();
DaoSession daoSession = new DaoMaster(db).newSession();
Now you can use the daoSession
to perform database operations.
Competitor Comparisons
Android Database - first and fast, lightweight on-device vector database
Pros of ObjectBox
- Higher performance, especially for bulk operations and queries
- Support for relations and eager fetching
- More modern API design with reactive extensions
Cons of ObjectBox
- Steeper learning curve due to more complex features
- Less mature ecosystem compared to GreenDAO
- Requires additional setup for native libraries
Code Comparison
ObjectBox:
@Entity
public class User {
@Id long id;
String name;
int age;
}
Box<User> userBox = store.boxFor(User.class);
userBox.put(new User(0, "John", 30));
GreenDAO:
@Entity
public class User {
@Id Long id;
String name;
int age;
}
UserDao userDao = daoSession.getUserDao();
userDao.insert(new User(null, "John", 30));
Both libraries offer similar entity definitions, but ObjectBox uses a Box concept for database operations, while GreenDAO uses DAOs. ObjectBox's API is generally more concise and offers more advanced features out of the box. However, GreenDAO's simpler approach may be easier for beginners to grasp and implement quickly.
Realm is a mobile database: a replacement for SQLite & ORMs
Pros of Realm Java
- Object-oriented database with real-time synchronization capabilities
- Supports encryption and multi-threading out of the box
- Faster query performance for complex operations
Cons of Realm Java
- Steeper learning curve due to its unique approach
- Larger APK size compared to GreenDAO
- Limited flexibility in database schema changes
Code Comparison
Realm Java:
Realm realm = Realm.getDefaultInstance();
realm.executeTransaction(r -> {
User user = r.createObject(User.class);
user.setName("John");
user.setAge(30);
});
GreenDAO:
User user = new User();
user.setName("John");
user.setAge(30);
daoSession.getUserDao().insert(user);
Both Realm Java and GreenDAO are popular ORM solutions for Android development. Realm Java offers a more modern approach with real-time synchronization and better performance for complex queries. However, it comes with a steeper learning curve and larger APK size. GreenDAO, on the other hand, is simpler to use and has a smaller footprint, but may not perform as well for complex operations. The choice between the two depends on the specific requirements of your project, such as the need for real-time sync, performance priorities, and development team familiarity.
requery - modern SQL based query & persistence for Java / Kotlin / Android
Pros of requery
- Supports multiple platforms (Android, Java SE, iOS) unlike greenDAO's Android-only focus
- More flexible query API with support for complex joins and subqueries
- Built-in RxJava support for reactive programming
Cons of requery
- Steeper learning curve due to more complex API
- Potentially slower compile times compared to greenDAO's annotation processing
- Less mature ecosystem and community support
Code Comparison
greenDAO example:
@Entity
public class User {
@Id private Long id;
private String name;
private int age;
}
requery example:
@Entity
public interface User {
@Key @Generated Long getId();
String getName();
int getAge();
}
Both libraries use annotations to define entities, but requery uses interfaces instead of classes. This approach allows for more flexibility in implementation but may require additional setup.
requery offers a more SQL-like query syntax:
Result<User> users = data
.select(User.class)
.where(User.AGE.gt(18))
.orderBy(User.NAME.asc())
.get();
While greenDAO uses a more method-chaining approach:
List<User> users = userDao
.queryBuilder()
.where(UserDao.Properties.Age.gt(18))
.orderAsc(UserDao.Properties.Name)
.list();
Both libraries provide efficient database operations, but requery's syntax may be more familiar to developers with SQL experience.
Insanely easy way to work with Android Database.
Pros of Sugar
- Simpler API and easier to set up for basic use cases
- Automatic table creation and schema updates
- More intuitive query syntax for developers familiar with SQL
Cons of Sugar
- Less performant for complex queries and large datasets
- Limited customization options for advanced ORM features
- Fewer optimization techniques compared to GreenDAO
Code Comparison
Sugar:
@Table
public class Book extends SugarRecord {
String title;
String author;
public Book(String title, String author) {
this.title = title;
this.author = author;
}
}
GreenDAO:
@Entity
public class Book {
@Id private Long id;
private String title;
private String author;
@Generated(hash = 1839243756)
public Book(Long id, String title, String author) {
this.id = id;
this.title = title;
this.author = author;
}
}
Sugar offers a more concise entity definition, while GreenDAO requires more boilerplate code but provides better control over entity structure and relationships.
A blazing fast, powerful, and very simple ORM android database library that writes database code for you.
Pros of DBFlow
- More flexible query building with its fluent API
- Supports multiple databases and custom database wrappers
- Offers more advanced features like migrations and model caching
Cons of DBFlow
- Steeper learning curve due to more complex API
- Potentially slower compile times with extensive annotation processing
- Less mature and smaller community compared to greenDAO
Code Comparison
DBFlow query example:
SQLite.select()
.from(MyTable::class.java)
.where(MyTable_Table.name.eq("John"))
.querySingle()
greenDAO query example:
List<User> users = userDao.queryBuilder()
.where(UserDao.Properties.Name.eq("John"))
.list();
Both libraries provide ORM functionality for Android, but DBFlow offers more flexibility and features at the cost of complexity. greenDAO is simpler to use and has a larger community, but may be less powerful for advanced use cases. DBFlow's query building is more expressive, while greenDAO's approach is more straightforward. The choice between them depends on project requirements and developer preferences.
ORMLite Android functionality used in conjunction with ormlite-core
Pros of ORMLite-Android
- More flexible and supports a wider range of databases
- Better documentation and community support
- Easier to integrate with existing projects
Cons of ORMLite-Android
- Slower performance compared to GreenDAO
- More complex setup and configuration
- Larger library size, which may impact app size
Code Comparison
GreenDAO:
@Entity
public class User {
@Id private Long id;
private String name;
private int age;
}
ORMLite-Android:
@DatabaseTable(tableName = "users")
public class User {
@DatabaseField(id = true)
private Long id;
@DatabaseField
private String name;
@DatabaseField
private int age;
}
Both libraries use annotations to define entity classes, but GreenDAO's syntax is slightly more concise. ORMLite-Android requires more explicit configuration, such as specifying the table name and field properties.
GreenDAO generally offers better performance and a smaller footprint, making it ideal for mobile applications with simpler database requirements. ORMLite-Android, on the other hand, provides more flexibility and broader database support, making it suitable for complex projects or those requiring cross-platform compatibility.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
â ï¸ This project is not longer actively maintained. If you are looking for an easy to use and efficient database solution, please:
Check out ObjectBox
Check out our new mobile database ObjectBox (GitHub repo).
ObjectBox is a superfast object-oriented database with strong relation support. ObjectBox is embedded into your Android, Linux, macOS, or Windows app.
greenDAO
greenDAO 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.
Home page, documentation, and support links: https://greenrobot.org/greendao/
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.
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:<agp-version>'
classpath 'org.greenrobot:greendao-gradle-plugin:3.3.1' // 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.
Continue at the Getting Started page.
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.**
Homepage, Documentation, Links
For more details on greenDAO please check greenDAO's website. Here are some direct links you may find useful:
More Open Source by greenrobot
ObjectBox is a new superfast object-oriented database for mobile.
EventBus is a central publish/subscribe bus for Android with optional delivery threads, priorities, and sticky events. A great tool to decouple components (e.g. Activities, Fragments, logic components) from each other.
Essentials is a set of utility classes and hash functions for Android & Java projects.
Top Related Projects
Android Database - first and fast, lightweight on-device vector database
Realm is a mobile database: a replacement for SQLite & ORMs
requery - modern SQL based query & persistence for Java / Kotlin / Android
Insanely easy way to work with Android Database.
A blazing fast, powerful, and very simple ORM android database library that writes database code for you.
ORMLite Android functionality used in conjunction with ormlite-core
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot