Monday, May 7, 2012

Analysing ORMs for Android



ActiveAndroid

https://www.activeandroid.com/
-Verbose
--Need to declare everything with annotations
--Query with too much method calls
-$19.90 / developer

android-active-record

http://code.google.com/p/android-active-record/
+No model configuration
+Annotation only when not following convention
-Database configuration before every access to database
-No relations support
-CRUD methods in _db object
--Manager like JPA EntityManager

Androrm

http://androrm.the-pixelpla.net/
-Always need to pass the ApplicationContext
-Need to call object's method that retrieves a QuerySet, where query methods are implemented
--Ex:
    Author.objects(getApplicationContext()).all();
+Create, update and delete methods called directly from Models (although passing applicationContext)
+Relations support
-Verbose model configuration
--Inspired by Django
--Ex:
    protected CharField mTitle;
    protected ForeignKeyField mAuthor;
   
    public Book() {
        super();
       
        mTitle = new CharField(80);
        mAuthor = new ForeignKeyField(Author.class);
    }
 

ORMLite

http://ormlite.com/sqlite_java_android_orm.shtml
-Uses dao for each model with crud methods
-Lack of documentation for Android
-Model configuration with annotations
--Ex:
    // you get the SQLiteOpenHelper from your Android Activity
    ConnectionSource connectionSource =
         new AndroidConnectionSource(sqliteOpenHelper);
    Dao accountDao =
         BaseDaoImpl.createDao(connectionSource, Account.class);
    TableUtils.createTable(connectionSource, Account.class);

    String name = "Jim Smith";
    Account account = new Account(name, "_secret");
    if (accountDao.create(account) != 1) {
         throw new Exception("Failure adding account");
    }
    Account account2 = accountDao.queryForId(name);
    connectionSource.close();

GreenDAO

http://greendao-orm.com/
-Lot of configuration
-It uses another app to create entities
+Relations support
-Programmatic model configuration
--EX:
    Entity user = schema.addEntity("User");
    user.addIdProperty();
    user.addStringProperty("name");
    user.addStringProperty("password");
    user.addIntProperty("yearOfBirth");
-Verbose query creation
--Ex:
    QueryBuilder qb = userDao.queryBuilder();
    qb.where(Properties.FirstName.eq("Joe"),
    qb.or(Properties.YearOfBirth.gt(1970),
    qb.and(Properties.YearOfBirth.eq(1970), Properties.MonthOfBirth.ge(10))));
    List youngJoes = qb.list();
 

Sugar

https://github.com/satyan/sugar
+Little configuration
-No relations support
-Need to pass context and class everytime
--Ex:
    # inserting values
    Note note = new Note(context, "Note 1");
    note.save();
    # query
    Note.findById(context, Note.class, 1);
    Note.find(context, Note.class, "title=?", new String[]{"Note 1"});
    # delete
    Note note = Note.findById(context, Note.class, 1);
    note.delete();
    # few more..
    Note.listAll(context, Note.class);
    Note.deleteAll(context, Note.class);
 

Orman

https://github.com/ahmetalpbalkan/orman
-No transaction
-Need to pass class everytime
+Few model configuration
+Create, update and delete methods called directly from Models
-Verbose query creation
--Ex:
    List students = Model.fetchAll(Student.class);

    String name = (String) Model.fetchSingleValue(ModelQuery.select()
                    .from(Student.class)
                    .orderBy("Student.gpa", "-Student.registrationDate")
                    .limit(1)
                    .selectColumn(Student.class, "name")
                    .getQuery());


Hadi

http://hadi.sourceforge.net/
-DAO
-Classes annotations
-Less used

 

Some comparison


  1. http://software-workshop.eu/content/comparing-android-orm-libraries-greendao-vs-ormlite
  2. http://www.ninthavenue.com.au/android-orm-jpa
  3. https://github.com/ahmetalpbalkan/orman/wiki/Why-orman-is-better-than-other-orms-for-you%3F


Which I'll use

As a Rails developer, I first tried to find something like ActiveRecord for Android. Actually I tried to create it first, then I thought that it should exists somewhere over the internet.

So I was looking for something easy to use and zero configuration, but as I tried to build it in Java, I saw that was impossible to do with Java what is done with Ruby, then I just discovered that many Frameworks.

At first, like I was searching for some ActiveRecord for Android, I found ActiveDroid and android-active-record, and a lot of others came during my research.

Finally, after creating this comparative post I decided to study deeper two of then: android-active-record and Sugar, just because they were simplest, and based in my original problem too, that was "How difficult is to add a new field in database and it show up on view".


That's it.

Edit: After studying both android-active-record and Sugar, I chose Sugar because of its zero configuration and simplicity.

2 comments:

  1. Thanks very much!!! Your research is very useful. Helps me a lot.

    ReplyDelete
  2. Thanks for the listing. You can add one more powerful Android ORM
    in your list and that is JDXA Android ORM. free to try http://www.softwaretree.com/v1/products/jdxa/jdxa.html

    ReplyDelete