Saving Complex Objects using Android Room Library

Save

In this blog, we will learn about saving complex objects using android room library.

If you have no knowledge about the android room, then you should first read this article:

Android Room Persistence Library

After reading this article, you get to know how easy now it is to create a table in your android application. It is just like creating a model class ( a Custom object).

This room library has saved us a lot from the boilerplate code that we used to write for creating a table.But when you start using in the real world scenarios, then you see that you have complex models which have one or more list of objects and some child objects. And room only recognizes the primitive datatypes.

Now you have two options :

  1. To restructure your model class and save the inner objects into separate tables and then manage them through relations.
    OR
  2. To use @Embedded annotation.

Before we go further, let’s first understand what this @Embedded annotation is and what it will do?

As per official docs: @Embedded can be used as an annotation on a field of an Entity or Pojo to signal that nested fields (i.e. fields of the annotated field’s class) can be referenced directly in the SQL queries.

What this means is that if you have some child object in your Model( Entity) class, then when creating the table, the number of columns will be automatically increased to fit in your child object as well. The total number of columns now will be ( Number of columns in your model class + number of columns(fields that make up your object) in your child object class).

But this will only help you save the child object and not the list as we have still not used anything that will help the room to identify the list of objects. for this purpose, we will use type converters.

Now, You will be wondering what exactly is a Type Converter?

Type Converter specifies additional type converters that Room can use. The TypeConverter is added to the scope of the element so if you put it on a class/interface, all methods/fields in that class will be able to use the converters.

Now enough of theory and explanation, let’s have a look at the coding part.

CODE :

Model(Object) Class: This class I need to save to my table.

User.class :

DataTypeConverter.class: This class will help Android room to convert the list into a primitive data type ( String) and save it in the table.

Ok, till now we understood the use of type converter, but still wondering what the @Embedded annotation did.

When Room will create your table.(my_table in our case). The table will have 4 columns ( 2 for the object that you are using and 2 for the embedded child object).

That’s it. Hope this helps you understand the room architecture and how you will be able to save the real world objects using room easily.

 

. . .
Discuss on Helpdesk

Leave a Comment

Your email address will not be published. Required fields are marked*


6 comments

  • Mehul Kadam
    What if I define User as an @Entity, will it save data separately in the User table instead of my_table?
    • anchit (Moderator)
      Hello Mehul,

      Yes, if you define User as an @Entity, then data of users will be saved in a separate table.

      But then you might have to use foreign keys for embedding the user refrence in your my_table .

      For more details, you can check the official documentation here –> https://developer.android.com/training/data-storage/room

      Hope this helps you 🙂

  • Aditi
    When we create my_table, shouldn’t Room create 4 columns instead of 5 : 2 from parent_table(id, List) and 2 from child table (first_name, last_name )?
    I suppose , ROOM shouldn’t create a column for mUser, bcoz, ROOM is creating columns for mUser’s fields namely first_name and last_name right?
    • anchit (Moderator)
      Yes, you are correct the table will have 4 columns.

      thanks for the correction, I will update this correction in the blog right away.

  • Hanna Papova
    Hello! Thank you for this article. But what if my child class also have lists and child classes in it? Should I use the annotation Embedded for all child’s child classes also and should I write Type converters for child’s List fields?
    • anchit (Moderator)
      Hi,
      This totally depends on the structure you want.
      If you have some scenario where you need to query the child key as well, then you should definitely use the embedded notations in your model calsses.

      But if you just want to save all of the children data in one class then you can have a typeconverter that converts all your child classes to a string and save that in your model class itself.

  • css.php
    Start a Project


      Message Sent!

      If you have more details or questions, you can reply to the received confirmation email.

      Back to Home