Android ORM / JPA

By , 19 December 2011

Android ORM / JPA
Android ORM / JPA

Coming from the server-side, I find it hard to live without ORM (Object-Relational Mapping) such as JPA. Now that I've started writing Android Apps, I set about finding a JPA ORM tool for Android.

Here is a comparison of the tools I found that do ORM and are lightweight enough for use with Android.

  Annotations DB Ops Size License Docs Age
OrmLite JPA or custom DAO or Entity 200kb Open docs 1yr
ORMAN JPA-like Entity 170kb Apache2 wiki 2yrs
greenDAO N/A Codegen DAO or Entity <100kb Apache2 docs  
ActiveAndroid JPA-like Entity   $20 wiki  
AndrORM None       docs 1yr

"DB Ops" refers to how database operations such as persist() and update() are handled. Normally either by a DAO (data access object) or by the entity themselves.

Handling Large Data Sets

Performance counts - especially since I need to handle large data sets. I don't want to load 3000 records into memory at once when the device only has room to display 10 at a time. Native Android supplies a Cursor to interate large data sets, and some ORM tools provide their own method too.

greenDAO has Query.lazyList() and OrmLite has DAO.iterator(Query) to do this, however Android adapters do not bind to an iterator so a custom adapter would need to be written in any case. There is a thread online that discusses this, however I think I can rig up a something similar to my LazyList for JPA to make this happen.

Verdict

I've started my project with OrmLite because it is the most mature. Lazy loading query results will need some custom code, but in the long run I think it will be better than polluting my code with column names and SQL hacks.

greenDAO was a good candidate from the performance perspective, but really I want to write my Entities myself so I can add the methods that I need.

Android ORM / JPA

About Roger Keays

Android ORM / JPA

Roger Keays is an artist, an engineer, and a student of life. He has no fixed address and has left footprints on 40-something different countries around the world. Roger is addicted to surfing. His other interests are music, psychology, languages, the proper use of semicolons, and finding good food.

Comment posted by: , 12 years ago

Jeff, the JPA annotations didn't have very good coverage of the ORMlite mappings and features and didn't offer much advantage over the native annotations so I used the native annotations.

Comment posted by: Jeff Bethune, 12 years ago

 Were you able to get the JPA annotations working with Ormlite on Android?  Or did you go with the ormlite annotations?

Comment posted by: , 12 years ago

Hi Phil. OrmLite is better documented than Androrm and seems to have a bigger community. That's all.

Comment posted by: Phil, 12 years ago

 Hey, what was the reason to decide against androrm? I'm the developer and very interested in any feedback, that could help me to improve androrm. By the way it is licensed under the MIT License. See androrm.com/download/license

Comment posted by: Sven Meier, 12 years ago

<shameless plug>

Take a look at:

   https://github.com/svenmeier/propoid

Age < 1 month, but it cursor-backed queries can easily be used with listviews.

</shameless plug>