v4.1.0 (Jan 6, 2017)
Nov 9, 2015
Feb 14, 2017
FUJI Goro (gfx)
FUJI Goro (gfx)
Yasuhiro Shimizu (yshrsmz)
Source code
APK file


Android Orma

Orma is an ORM (Object-Relation Mapper) for Android SQLiteDatabase, generating helper classes at compile time with annotation processing, inspired in ActiveAndroid, GreenDAO, and Realm.

The interface of Orma is very simple and easy to use, as the author respects the Larry Wall's wisdom:

Easy things should be easy, and hard things should be possible -- Larry Wall

Table of Contents


There are already a lot of ORMs. Why I have to add another wheel?

The answer is that I need ORM that have all the following features:

  • Fast as hand-written code
  • POJO models
    • Model classes should have no restriction
    • Might implement Parcelable and/or extend any classes
    • They should be passed to another thread
  • A database handle must be an object instance
    • Not a singleton class
    • Not a static-method based class
  • Easy migration
    • Some ALTER TABLE, e.g. add column and drop column, should be detectd and processed
    • There is a wheel in Perl: SQL::Translator::Diff
  • Code completion friendly
    • db.selectFromModel() is better than new Select(Model.class)
  • Custom raw queries are sometimes inevitable
    • GROUP BY ... HAVING ...
    • SELECT max(value), min(value), avg(value), count(value) FROM ...

And now they are what Orma has.


  • JDK 8 (1.8.0_66 or later) to build
  • Android API level 15 to use

Getting Started

Declare dependencies to use Orma and its annotation processor.

dependencies {
    annotationProcessor 'com.github.gfx.android.orma:orma-processor:4.2.0'
    compile 'com.github.gfx.android.orma:orma:4.2.0'

NOTE: if you use Android Gradle Plugin before 2.2.0, you must use android-apt plugin instead of annotationProcessor configuration.


First, define model classes annotated with @Table, @Column, and @PrimaryKey and run the Build APK command to generate helper classes.

package com.github.gfx.android.orma.example;

import com.github.gfx.android.orma.annotation.Column;
import com.github.gfx.android.orma.annotation.PrimaryKey;
import com.github.gfx.android.orma.annotation.Table;

import android.support.annotation.Nullable;

public class Todo {

    public long id;

    @Column(indexed = true)
    public String title;

    @Nullable // allows NULL (default: NOT NULL)
    public String content;

    public long createdTimeMillis;

Second, instantiate a database handle OrmaDatabase, which is generated by orma-processor.

Here is an example to configure OrmaDatabase:

// See OrmaDatabaseBuilderBase for other options.
OrmaDatabase orma = OrmaDatabase.builder(context)

Then, you can create, read, update and delete models via OrmaDatabase:

Todo todo = ...;

// create

// prepared statements with transaction
orma.transactionSync( -> { // or transactionAsync() to execute tasks in background
    Inserter<Todo> inserter = orma.prepareInsertIntoTodo();

// read
  .titleEq("foo") // equivalent to `where("title = ?", "foo")`
  .executeAsObservable() // first-class RxJava interface

// update
  .content("a new content") // to setup what are updated

// delete

The Components

Database Handles

A database handle, named OrmaDatabase by default, is generated by orma-processor, which is an entry point of all the high-level database operations.

This is typically used as a singleton instance and you don't need to manage its lifecycle. That is, you don't need to explicitly close it.


A model in Orma is a Java class that is annotated with @Table, which has at least one column, a field annotated with @Column or @PrimaryKey.

orma-processor generates helper classes for each model: Schema, Relation, Selector, Updater, and Deleter.

Because these helper classes are generated at the compile time, you can use Orma as a type-safe ORM.

Schema Helpers

A Schema helper, e.g. Todo_Schema, has metadata for the corresponding model.

This is an internal helper class and not intended to be employed by users.

Relation Helpers

A Relation helper, e.g. Todo_Relation, is an entry point of table operations.

This is created by a database handle:

public static Todo_Relation relation() {
  return orma.relationOfTodo();

And is able to create Selector, Updater, Deleter, and Inserter for the target model.

Todo_Relation todos = orma.relationOfTodo();

todos.selector().toList(); // Todo_Selector
todos.updater().content("foo").execute(); // Todo_Updater
todos.inserter().execute(todo); // Inserter<Todo>
todos.deleter().execute(); // Todo_Deleter

This can be a subset of a table which has ORDER BY clauses and WHERE clauses with some List-like methods:

Todo_Relation todos = orma.relationOfTodo()
  .doneEq(false) // can have conditions
  .orderByCreatedTimeMillis(); // can have orders

// List-like features:
int count = todos.count();
Todo todo = todos.get(0);

// Convenience utilities
int position = todos.indexOf(todo);
  .subscribe(position -> {
    notifyItemRemoved(position); // assumes Adapter#notifyItemRemoved()

// Todo_Relation implements Iterable<Todo>
for (Todo todo : todos) {
  // ...

And has convenience #upsert() to "save it anyway", returning a new model:

Todo_Relation todos = orma.relationOfTodo()

Todo newTodo = todos.upsert(todo); // INSERT if it's not persistent; UPDATE Otherwise

Unlike INSERT with OnConflict.REPLACE, #upsert() doesn't break associations.

NOTE: if you use a model after #upsert(), you must use the returned newModel. This is because Orma does not change the model's primary key on INSERT.

Selector Helpers

A Selector helper, e.g. Todo_Selector, is created by a Relation:

Todo_Selector selector = relation().selector();
// or orma.selectFromTodo();

This is a query builder for SELECT ... FROM * statements.

Updater Helpers

An Updater helper, e.g. Todo_Updater, is created by a Relation:

Todo_Updater updater = relation().updater();
// or orma.updateTodo();

This is a query builder for UPDATE * statements.

Deleter Helpers

A Deleter helper, e.g. Todo_Deleter, is created by a Relation:

Todo_Deleter deleter = relation().deleter();
// or orma.deleteFromTodo();

This is a query builder for DELETE FROM * statements.

Query Helper Methods

There are Query Helpers which are generated to query conditions and orders in a type-safe way.

For example, titleEq() shown in the synopsis section, are generated to help make WHERE and ORDER BY clauses, for Relation, Selecotr, Deleter, and Updater.

Most of them are generated for columns with indexed = true, and some are for @PrimaryKey columns.

List of Query Helper Methods

Here is a list of Query Helpers that are generated for all the indexed columns, where * is a column name pladeholder:

Method SQL
*Eq(value) * = value
*NotEq(value) * <> value
*In(values) * IN (values)
*NotIn(values) * NOT IN (values)

The following are generated for @Nullable columns.

Method SQL
*IsNull() * IS NULL
*IsNotNull() * IS NOT NULL

The following are generated for numeric columns (i.e. byte, short, int, long, float, double, and their corresponding box types)

Method SQL
*Lt(value) * < value
*Le(values) * <= value
*Gt(value) * > value
*Ge(value) * >= value
*Between(a, b) * BETWEEN a AND b

And ORDER BY helpers:

Method SQL
orderBy*Asc() ORDER BY * ASC
orderBy*Desc() ORDER BY * DESC

How to Control Generation of Query Helpers

This is an advanced setting for those who know what they do.

You can control which Query Helpers are generater for a column by @Column(helpers = ...) attribute:

    helpers = Column.Helpers.AUTO // default to AUTO

Here are the definition of options defined in Column.java:

long AUTO = -1; // the default, a smart way
long NONE = 0;

long CONDITION_EQ = 0b01;






The Inserter Helpers

This is a prepared statement for INSERT INTO ... for bulk insertions.

Inserter<Todo> inserter = relation().inserter();
// or orma.insertIntoTodo()


Details of Database Handles

The section describes the details of database handles.

Configuration of Database Handles

The database class is configured by the @Database annotation:

    databaseClassName = "OrmaDatabase", // default to "OrmaDatabase"
    includes = { /* ... */ } // Give model classes to handle
    excludes = { /* ... */ } // Give model classes not to handle
public class DatabaseConfiguration { }

The annotated class is not used for now, but the package is used to place the OrmaDatabase class.

Database Handle Builders

OrmaDatabase.builder(Context) returns a builder isntance, which has configure the database handle instance:

Method Description Default
name(String) The filename of SQLite DB "${package}.orma.db"
migrationEngine(MigrationEngine) Custom migration engine OrmaMigration
writeAheadLogging(boolean) SQLite WAL flag true
foreignKeys(boolean) SQLite FOREIGN_KEYS flag true
migrationStep(int, ManualStepMigration.Step) A migration step none
trace(boolean) Output executed queries to logcat if true dynamic (*1)
readOnMainThread(AccessThreadConstraint) Check read operation on main thread dynamic (*2)
writeOnMainThread(AccessThreadConstraint) Check write operation on main thread dynaimc (*3)
  • *1 BuildConfig.DEBUG ? true : false
  • *2 BuildConfig.DEBUG ? WARN : NONE
  • *3 BuildConfig.DEBUG ? FATAL : NONE

Note that Orma aborts if writing occurs on main thread in debug build.

Use background threads, e.g. via AsyncTask for writing, or RxJava interfaces with Schedulers.io().

Otherwise you can disable this behavior:

OrmaDatabase orma = OrmaDatabase.builder(context)

In-Memory Database

You can create in-memory databases by passing null to OrmaDatabase.Builder#name().

This is useful for testhing.

Details of Models

The section describes the details of model definition.

Setters and Getters

Orma can use getters and setters if columns have corresponding methods.

You can also connect getters and setters with @Getter and @Setter respectively, which tells orma-processor to use accessors.

Each accessor name can have a column name in SQLite databases, which is inferred from its method name if omitted.

public class KeyValuePair {

    static final String kKey = "Key";

    @Column(kKey) // specifies the name
    private String key;

    @Column // omits the name
    private String value;

    public String getKey() {
        return key;

    public void setKey(String key) {
        this.key = key;

    // used as a getter for the "value" column
    // @Getter is optional in this case
    public String getValue() {
        return value;

    // used as a setter for the "value" column
    // @Setter is optional in this case
    public void setValue(String value) {
        this.value = value;

Immutable Models

Immutable models, where all the fields are declared with final, are supported by annotating a constructor with @Setter.

public class KeyValuePair {

    public final String key;

    public final String value;

    KeyValuePair(String key, String value) {
        this.key = key;
        this.value = value;

It can be declared with custom names:

public class KeyValuePair {
    static final String kKey = "Key";
    static final String kValue = "Value";

    public final String key;

    public final String value;

    KeyValuePair(@Setter(kKey) String key, @Setter(kValue) String value) {
        this.key = key;
        this.value = value;

Composite Indexes

There is the indexes parameter that @Table takes in order to create composite indexes (a.k.a. multi-column indexes).

@Table(indexes = @Index(value = {"resourceType", "resourceId"}))
public class Entry {

    public long id;

    public String resourceType;

    public long resourceId;
    indexes = @Index(
                value = {"resourceType", "resourceId"},
                unique = true
public class Entry {

    public long id;

    public String resourceType;

    public long resourceId;

Composite indexes generate query helper methods only for == and ORDER BY for helper classes like the following:

  • Selector#resourceTypeAndResourceIdEq(String, long)
  • Selector#orderByResourceTypeAndResourceIdAsc()
  • Selector#orderByResourceTypeAndResourceIdDesc()

You can control generated helpers with the helpers parameter.

See also Query Helper Methods.

Reserved Names

Column names starting $ are reserved for metadata.

Other names, including SQLite keywords, are allowed.

RxJava Integration

RxJava integration provides a set of powerful API to transform, filter, and combine DB rows.

For example, there is a model named Book with @Column(unique = true) String title and you'd like to get a Map<String, Book> where the key is Book#title:

Map<String, Book> map = db.selectFromBook()
        .toMap(new Function<Book, String>() {
            public String apply(Book book) throws Exception {
                return book.title;


Two or more Orma models can be associated with association mechanism.

There are two type of associations: has-one and has-many.

In addition, there are another two kind of association supports: indirect associations with SingleAssociation<T> and direct associations.

Has-One Associations with SingleAssociation<T>

There is SingleAssociation<T> to support has-one associations, which is retrieved on demand; this is useful if the associations are not always used.

For example, a book has a publisher:

class Publisher {
  public long id;

class Book {

    public SingleAssociation<Publisher> publisher;

To save this a book:

Book book = new Book();
Publisher publisher = new Publisher();

long publisherId = db.insertIntoPublisher()

// if publisher has a valid primary key, `just(publisher)` is also okay.
book.publisher = SingleAssociation.just(publisherId);


To get a publisher from books:

    .forEach((book) -> {
        // blocking:
        Publisher publisher = book.publisher.get();

        // with RxJava Single<Publisher>:
            .subscribe((publisher) -> {
                // use publisher

Direct Associations

There are direct associations, where an Orma model has another Orma model directly.

Given a has-one association, Book has-one Publisher:

class Publisher {
  public long id;

  public String name;

class Book {

    public long id;

    public String title;

    public Publisher publisher;

The corresponding table definition is something like this:

CREATE TABLE `Publisher` (
  `name` TEXT NOT NULL
  `title` TEXT NOT NULL,
  `publisher` INTEGER NOT NULL

In SQL, Book#publisher refers Publisher#id, indicating the two tables should be joined in SELECT statements.

In Java, Book#publisher is a Publisher instance, which is retrieved in each SELECT operations. There is no lazy loading in direct associations.

Has-Many Associations with SingleAssociation<T>

Has-many associations are not directly supported but you can define a method to get associated objects:

class Publisher {
    public long id;

    // NOTE: If OrmaDatabase is a singleton, no parameter is required!
    public Book_Relation getBooks(OrmaDatabase db) {
        return db.relationOfBook().publisherEq(this);

class Book {

    @Column(indexed = true)
    public SingleAssociation<Publisher> publisher;


Has-Many Associations with Direct Associations

As SingleAssociation is, you can define a helper method to get has-many associations:

class Publisher {
    public long id;

    // NOTE: If OrmaDatabase is a singleton, no parameter is required!
    public Book_Relation getBooks(OrmaDatabase db) {
        return db.relationOfBook().publisherEq(this);

class Book {

    @Column(indexed = true)
    public Publisher publisher;


Limitations in Associations

  • There are no methods to query associated models

These issues will be fixed in a future.

Type Adapters

Orma models are able to have embedded objects with type adapters, a.k.a. static type adapters, by defining classes with @StaticTypeAdapter annotation.

For example, if you want to embed LatLng in your Orma model, you can define a type adapter like this:

    targetType = LatLng.class, // required
    serializedType = String.class // required
public class LatLngAdapter {

    // SerializedType serialize(TargetType source)
    public static String serialize(@NonNull LatLng source) {
        return source.latitude + "," + source.longitude

    // TargetType deserialize(SerializedType serialized)
    public static LatLng deserialize(@NonNull String serialized) {
        String[] values = serialized.split(",");
        return new LatLng(

@StaticTypeAdapter requires targetType and serializedType options and two static methods SerializedType serialize(TargetType) and TargetType deserialize(SerializedType).

How Serialized Types Used

A @StaticTypeAdapter#serializedType is bound to an SQLite storage type. Thus it must be one of the "Java Type" listed the table below, where each "Java Type" has a corresponding "SQLite Type":

Java Type SQLite Type
boolean INTEGER
float REAL
double REAL
String TEXT
byte[] BLOB

@StaticTypeAdapters for Multiple Serializers at Once

You can also define multiple type serializers to single class with @StaticTypeAdapters annotation containers:

        targetType = MutableInt.class,
        serializedType = int.class,
        serializer = "serializeMutableInt",
        deserializer = "deserializeMutableInt"
        targetType = MutableLong.class,
        serializedType = long.class,
        serializer = "serializeMutableLong",
        deserializer = "deserializeMutableLong"
public class TypeAdapters {

    public static int serializeMutableInt(@NonNull MutableInt target) {
        return target.value;

    public static MutableInt deserializeMutableInt(int deserialized) {
        return new MutableInt(deserialized);

    public static long serializeMutableLong(@NonNull MutableLong target) {
        return target.value;

    public static MutableLong deserializeMutableLong(long deserialized) {
        return new MutableLong(deserialized);

Built-In Type Adapters

There are built-in type adapters for typically used value objects and collections:

  • java.math.BigDecimal
  • java.math.BigInteger
  • java.nio.ByteBuffer
  • java.util.Currency
  • java.util.Date
  • java.sql.Date
  • java.sql.Time
  • java.sql.Timestamp
  • java.util.UUID
  • java.util.List<String>
  • java.util.ArrayList<String>
  • java.util.Set<String>
  • java.util.HashSet<String>
  • android.net.Uri

Generic Type Adapters

If your deserialize() takes a Class<T> parameter, the type serializer is generic, handling classes with the common base classe.

For example, if you have some enums that implement EnumDescription, e.g. T extends Enum<T> & EnumDescription, you can handle it with a generic type adapter.

Given an interface EnumDescription:

public interface EnumDescription {

    long getValue();

And here is its type adapter:

        targetType = EnumDescription.class,
        serializedType = long.class
public class EnumTypeAdapter {

    public static <T extends Enum<T> & EnumDescription> long serialize(@NonNull T value) {
        return value.getValue();

    public static <T extends Enum<T> & EnumDescription> T deserialize(long serialized, @NonNull Class<T> type) {

        for (T enumValue : type.getEnumConstants()) {
            if (enumValue.getValue() == serialized) {
                return enumValue;

        throw new RuntimeException("Unknown id: " + serialized + " for " + type);

Now deserialize() uses the type information for the conclete target class.

Raw Queries

For low-level operations, e.g. executing a raw query, you can use OrmaDatabase#getConnection(), which returns OrmaConnection.

For example:

Cursor cursor = db.getConnection().rawQuery("SELECT max(bookId) as max_id, min(bookId) as min_id FROM Book");
// get data from cursor

NOTE: Don't use rawQuery() for performance because Orma query builders are fast enough.


There is a pluggable migration mechanism via the MigrationEngine interface.

The default migration engine is SchemaDiffMigration, which handles schema changes by making diff with old and new DDL stored in sqlite_master. That is, you don't need migration steps for the following cases:

  • Adding tables
  • Adding columns
  • Changing column types
  • Changing column constraints ( NOT NULL, UNIQUE, and etc.)

Of course, you can define migration steps for each schema version (or BuildConfig.VERSION).

Here is an example to define migration steps:

int VERSION_2; // a past version of VERSION_CODE

OrmaDatabase orma = OrmaDatabase.builder(this)
        .migrationStep(VERSION_2, new ManualStepMigration.ChangeStep() {
            public void change(@NonNull ManualStepMigration.Helper helper) {
              Log.(TAG, helper.upgrade ? "upgrade" : "downgrade");
              helper.execSQL("DROP TABLE foo");
              helper.execSQL("DROP TABLE bar");
        // ... other configurations

See migration/README.md for details.

DataSet Changed Events

NOTE: This is experimental in v4.2.0: its existence, signature or behavior might change without warning from one release to the next.

Relation#createQueryObservable() can create a event stream to observe data-set changed events for the relation.

This likes SQLBrite's' "Query Observable", whereas Orma's does not notify the initial event.

// NOTE: Keep the observable instance. If it's released, the observable is disposed.

// create a query observable, which is a hot observable
Observable<Author_Selector> observable = db.relationOfAuthor()

// subscribe the events
observable.flatMap(new Function<Author_Selector, Observable<Author>>() {
    public Observable<Author> apply(Author_Selector selector) throws Exception {
        Log.d(TAG, "Author has been changed!");
        return selector.executeAsObservable();
        .map(new Function<Author, String>() {
            public String apply(Author author) throws Exception {
                return author.name;
        .subscribe(new Consumer<String>() {
            public void accept(String name) throws Exception {
                Log.d(TAG, "name: " + name);

See OrmaListAdapter and OrmaRecyclerViewAdapter, which use Query Observables to trigger #notifyDataSetChanged().

Cooperation with Serialization Libraries

Beause Orma reuqires nothing to do to models, serializers, e.g. Android Parcels or GSON, can serialize Orma models.


There is an example app to demonstrate:

  • Migration
  • Orma with RecyclerView / ListView
  • Benchmark (see below)


There is a simple benchmark with Realm and hand-written SQLiteDatabase code:


Here is a result performed on Android 6.0.0 / Xperia Z4 as of Orma v4.2.0 and Realm 2.3.0, processing 10 items x 100 times:

I welcome benchmark in another condition and/or another code.

Method Count

Orma runtime is very lightweight: Method Count for v4.2.0


Can't build my project.

Check your toolchain. FYI here are my toolchain versions:

  • JDK 1.8.0_66
  • Android SDK Tools 25 or later
  • Android SDK Platform Tools 24 or later
  • Android SDK Build Tools 24 or later
  • Android Gradle Plugin 2.2.0 or later

How can I enable debug logging on release build?

Call OrmaDatabase.Builder#trace(boolean) with true:

OrmaDatabase orma = OrmaDatabase.builder(context)

This option also enables logging in the default migration engine.

If you give a custom migration engine to the orma builder, you have to enable trace flag to its constructor:

boolean trace = true;
SchemaDiffMigration migration = new SchemaDiffMigration(context, trace);

How can see the generated Java files?

As other annotation processors do, Orma save files to $modle/build/generated/source/apt/.

You can see generated files for example models.

Does Orma work with Kotlin?

Yes, but it's experimental. Here is an example to use Orma with Kotlin:


NOTE: Kotlin APT support, a.k.a. kapt, is really unstable. Don't ask me how to solve kapt problems.

Does Orma work with the Jack compiler?

Yes. As of Android Gradle Plugin 2.2.2, Orma should work with Jack.

dependencies {
    annotationProcessor 'com.github.gfx.android.orma:orma-processor:4.2.0'
    compile 'com.github.gfx.android.orma:orma:4.2.0'

See https://github.com/gfx/OrmaWithJack for a working example.

When the database handle is opened and closed?

Orma opens the database handle in instantiating OrmaDatabase, and you don't need to close it.

In other word, you can define the database handle as a singleton instance in your application scope, and forget close.

Who uses Orma?

Here is a list of open-source Androdi apps using Orma which are released to Google Play:

Or you can search use cases by GitHub search.

Also, here is a list of apps using Orma which are proprietary:

Tell me if your projects use Orma!


Licenses in Runtime Dependencies


Patches are welcome!

Release Engineering for Maintainers

./gradlew bumpMajor # or bumpMinor / bumpPatch
git add -va
make publish # run tests, build artifacts, publish to jcenter, and make a tag

Visual Studio Code (a.k.a. vscode) is recommended to edit README.md and CHANGELOG.md. Especially the ToC section is managed by AlanWalk/Markdown-TOC.

See Also

Authors and Contributors

FUJI Goro ( gfx).

And contributors are listed here: Contributors


Copyright (c) 2015 FUJI Goro (gfx).

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at


Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.