A.I, Data and Software Engineering

Advanced android room persistent storage


Room provides an abstraction layer over SQLite to allow fluent database access while harnessing the full power of SQLite. Because Room takes care of several concerns for you, e.g. caching, networking, Android highly recommend using Room instead of SQLite. If you are coming from iOS, the good news is that Room is very similar to the core data in iOS development.

The task

Field nameType
User table

We will create a user data model, similar to the one from Android documentation website. Nevertheless, we will add more complicated data to the model as well as some queries.

The user model will look like this.

Getting started

To use Room in your app, add the following dependencies to your app’s build.gradle file:KOTLINJAVA

First you will need to add kapt to your project build.gradle:

Secondly, you will add the room’s dependencies.

Sync your project before creating the data model.

room architecture
room architecture

Implement data model

With Room, we use all annotation, e.g. @Entity to denote the class as a data entity (table), @PrimaryKey for the primary key. The Room framework will handle the table creation for you.

data access objects (dao)

To access the table created above, we need to declare a DAO class. The following code provides the standard methods in the database, i.e. insert, delete, select. Similarly, we use @Query to define the custom query that we want to assign to the method, use @Delete to create standard delete user.


The database to store these tables can be defined as follow

Address the uncommon data types

Now if you try to compile your app, you will have the following error

It is because there are data types, e.g. Bitmap and LocalDate that Room does not know how to store it automatically. To solve this, we need to provide relevant methods for retrieve and save these data. We use @TypeConverter for these methods.

Now we can update the Appdatabase with the annotataion @TypeConverters as follow

It should compile normally.

Define relations

If you have another table, e.g. Library and you want to set user-library relations

To do this, create a new data class where each instance holds an instance of the parent entity and the corresponding instance of the child entity. Add the @Relation annotation to the instance of the child entity, with parentColumn set to the name of the primary key column of the parent entity and entityColumn set to the name of the column of the child entity that references the parent entity’s primary key.

Tips for Some other problems

  • If your project failed to build, it is likely that you have issue with Dao class. Check it thoroughly.
  • If you have problem with schema, you can add to defaultConfig of build.gradle


Add comment

A.I, Data and Software Engineering

PetaMinds focuses on developing the coolest topics in data science, A.I, and programming, and make them so digestible for everyone to learn and create amazing applications in a short time.

Pin It on Pinterest


You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

Petaminds will use the information you provide on this form to be in touch with you and to provide updates.