The following is the first few sections of a chapter from Android's Architecture Components, plus headings for the remaining major sections, to give you an idea about the content of the chapter.
Google describes Room as providing “an abstraction layer over SQLite to allow fluent database access while harnessing the full power of SQLite.”
In other words, Room aims to make your use of SQLite easier, through a lightweight annotation-based implementation of an object-relational mapping (ORM) engine.
If you have ever worked with a relational database — like SQLite — from an object-oriented language — like Java — undoubtedly you have encountered the “object-relational impedance mismatch”. That is a very fancy way of saying “gosh, it’s a pain getting stuff into and out of the database”.
In object-oriented programming, we are used to objects holding references to other objects, forming some sort of object graph. However, traditional SQL-style relational databases work off of tables of primitive data, using foreign keys and join tables to express relationships. Figuring out how to get our Java classes to map to relational tables is aggravating, and it usually results in a lot of boilerplate code.
Traditional Android development uses
SQLiteDatabase for interacting with SQLite.
That, in turn, uses
Cursor objects to represent the results of queries and
ContentValues objects to represent data to be inserted or updated. While
ContentValues are objects, they are fairly generic, much in the way that
ArrayList is generic. In particular, neither
ContentValues has any of our business logic. We have to somehow either wrap
that around those objects or convert between those objects and some of ours.
That latter approach is what object-relational mapping engines, or ORMs, take. A typical ORM works off of Java code and either generates a suitable database structure or works with you to identify how the Java classes should map to some existing table structure (e.g., a legacy one that you are stuck with). The ORM usually generates some code for you, and supplies a library, which in combination hide much of the database details from you.
The quintessential Java ORM is Hibernate. However, Hibernate was developed with server-side Java in mind and is not well-suited for slim platfoms like Android devices. However, a vast roster of Android ORMs have been created over the years to try to fill that gap. Some of the more popular ones have been:
Room also helps with the object-relational impedance mismatch. It is not as deep of an ORM as some of the others, as you will be dealing with SQL a fair bit. However, Room has one huge advantage: it is from Google, and therefore it will be deemed “official” in the eyes of many developers and middle managers.
While this book is focused on the Architecture Components — and Room is part of those — you may wish to explore other ORMs if you are interested in using Java objects but saving the data in SQLite. Room is likely to become popular, but it is far from the only option.
The preview of this section is in an invisible, microscopic font.
The preview of this section is unavailable right now, but if you leave your name and number at the sound of the tone, it might get back to you (BEEEEEEEEEEEEP!).
The preview of this section is sleeping in.