6 Best Android ORM Libraries

Object Relational Mapping (ORM) libraries are essential tools for any Android developer. They allow developers to easily map objects and data to a database, making it easier to store and retrieve information.

ORM libraries can also help developers to create more efficient and robust applications. In this article, we will explore the six best Android ORM libraries available, and how they can help you create better applications.

Room

The Room persistence library provides an abstraction layer over SQLite. It allows developers to access databases more robustly while harnessing the full power of SQLite without boilerplate.

Room checks SQL queries at the compile time, which preserves your app from crash issues at runtime. It can check syntax and missing tables. Furthermore, it works pretty well with other Architecture components (like LiveData).

@Entity(tableName = “user”)
public class User {

@PrimaryKey(autoGenerate = true)
private int uid;

@ColumnInfo(name = “first_name”)
private String firstName;

@ColumnInfo(name = “last_name”)
private String lastName;
//getters and setters go here
}

OrmLite

Object Relational Mapping Lite (OrmLite) is a library for providing some simple, lightweight functionality for persisting Java objects to SQL databases. It avoids the complexity and overhead of more standard ORM packages, hence Lite in the name.

It supports SQLite with native calls to Android OS database APIs. Data class can be configured by simply adding Java annotations. It also provides powerful abstract Database Access Object (DAO) classes.

@DatabaseTable(tableName = “account”)
public class Account {
@DatabaseField(id = true)
private String username;

@DatabaseField(canBeNull = false)
private String password;

Account() {
// a must defined no-arg constructor
}
public Account(String username, String password) {
this.username = username;
this.password = password;
}
//…getter and setter methods can go below
}

ActiveAndroid

ActiveAndroid is an active record style ORM which allows you to save and retrieve SQLite database records without ever writing a single SQL statement. The library helps you reduce lots of activities by taking care of all the setup with some simple configurations.

All queries use the query builder syntax or the Model.query() method. Each database record is wrapped neatly into a class with methods like save() and delete().

@Table(name = “Books”)
public class Item extends Model {
@Column(name = “Name”)
public String name;

@Column(name = “Price”)
public float price;

@Column(name = “Publisher”)
public String publisher;
}

requery

Requery is a light but powerful object mapping and SQL generator for Java/Kotlin/Android with RxJava and Java 8 support. You can easily map to or create databases, and perform queries and updates from any platform that uses Java.

Relationships such as One-to-One, One-to-Many, Many-to-One, and Many-to-Many can be defined in your models using annotations. 

@Entity
abstract class AbstractPerson {

    @Key @Generated
    int id;

    @Index("name_index")                     // table specification
    String name;

    @OneToMany                               // relationships 1:1, 1:many, many to many
    Set<Phone> phoneNumbers;

    @Converter(EmailToStringConverter.class) // custom type conversion
    Email email;

    @PostLoad                                // lifecycle callbacks
    void afterLoad() {
        updatePeopleList();
    }
    // getter, setters, equals & hashCode automatically generated into Person.java
}

DBFlow

DBFlow is a fast, efficient, and feature-rich Kotlin database library built on SQLite for Android. It utilizes annotation processing to generate SQLite boilerplate for you and provides a powerful SQLite query language that makes using SQLite a joy.

Since it is written in Kotlin, it supports Coroutines. That means you can query using a coroutine. It also supports paging via QueryDataSource, database encryption with SQLCipher, and RX Java.

greenDAO

Writing SQL and parsing query results in SQLite are quite tedious and time-consuming tasks. greenDAO frees you from these by mapping Java objects to a database table. Then actions like store, update, delete, and query for Java objects become a simple object-oriented API.

Entity account = schema.addEntity(“Account”);
account.addIdProperty();
account.addStringProperty(“username”);
account.addStringProperty(“password”);

Many top Android apps rely on this library for database activities.

Leave a Comment

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


Scroll to Top