Room Database| Android Jetpack

Vibha Thakur
4 min readFeb 21, 2021

“Android Jetpack” this word always creates an image of “jet” in my mind🤷‍♀️.

So Android Jetpack is a collection of Android software components that make your development easier so that you can develop Android apps with amazing architecture and with other important aspects of a great application.

Watch the video for a better understanding.

They will help you achieve the following things easily:-

  • Follow best practices
  • Free you from writing boilerplate code.
  • Simplify complex tasks, so you can focus on the code you care about.

So, we use Room Persistence library but why 🤔🤷‍♂️

Room is an ORM, Object Relational Mapping library. In other words, Room will map our database objects to Java objects.

The Room persistence library provides an abstraction layer over SQLite to allow fluent database access while harnessing the full power of SQLite. In particular, Room provides the following benefits:

  • Compile-time verification of SQL queries.
  • Convenience annotations that minimize repetitive and error-prone boilerplate code.
  • Streamlined database migration paths.
  • Room provides easy and efficient Caching mechanism Apps that handle non-trivial amounts of structured data can benefit greatly from persisting that data locally. The most common use case is to cache relevant pieces of data so that when the device cannot access the internet, the user can still browse that content while they are offline so it is really cool.

😎 I was thinking to build a simple, usable but beautiful application for myself. then I realize I want a notes application in which I can save my notes and my poetry 🤷‍♂️.

So I decided to look on youtube and found an amazing series of complete notes application using the Room persistence library, but then I thought I don't know much about Android jetpack and its components as every blog articles and tutorial series is being taught in Kotlin and I develop in java at least for now.

Hence I decided to read blogs and about Room Persistence Library and till now I have a decent knowledge of the topic.

So here's what I have learned so far 😏

ROOM vs SQLite:-

Meanwhile SQLite

So the basic difference between Room and Sqlite is where SQLite deals with raw queries in Room there are not any raw queries.

A most important difference is that in SQLite there is not any compile-time verification of raw SQL queries whereas in Room there is SQL validation at compile time. So while dealing with Sqlite we need to deal with a lot of boilerplate code to convert SQL queries into java objects but in Room maps database objects and java objects without boilerplate code.

Yes you are awesome Room ❤

Also, SQLite API is low level so it takes more time and effort to build applications. wheres Room when used with ViewModel and live data make it easy and fast.

As your schema changes, you need to update the affected SQL queries manually. Room solves this problem.

Now let’s talk about complements of Room persistence library

1. Entity

Represents a table within the database. Room creates a table for each class that has @Entity annotation, the fields in the class correspond to columns in the table. Therefore, the entity classes tend to be small model classes that don’t contain any logic.

Annotations

Before we get started with modeling our entities, we need to know some useful annotations and their attributes.

@Entity — every model class with this annotation will have a mapping table in DB

2. Dao

DAOs are responsible for defining the methods that access the database. In the initial SQLite, we use the Cursor objects. With Room, we don’t need all the Cursor related code and can simply define our queries using annotations in the Dao class.

3. Database

Contains the database holder and serves as the main access point for the underlying connection to your app’s persisted, relational data.

To create a database we need to define an abstract class that extends RoomDatabase. This class is annotated with @ Database lists the entities contained in the database, and the DAOs which access them.

Ok that’s it for now in the next part we will add dependencies and perform CRUD operation.

--

--