Skip to content
Advertisement

Android Room Persistence Library : What is the best way to implement a many to many relation?

I used to use Realm and I am currently testing Room in order to compare both tools.

I am trying to implement the following many to many relation :

enter image description here

Here my Entity classes :

The Person :

JavaScript

The Cat class :

JavaScript

And the PersonCat class :

JavaScript

I also have a POJO in order to manipulate a person with cats into my app :

JavaScript

The question is : how to save a List<RoomPersonWithAnimals> ?

Should I do 3 requests each time in order to save :

  • the person into the table Person
  • the cats into the table Cat
  • its cats into the table PersonCat

Here the java code that illustrate the 3 requests :

JavaScript

In Realm, it’s possible to save in only one request these data. Is is a limitation of Room or my implementation is wrong ?

Thank you in advance for your help !

Advertisement

Answer

Since Room 2.2, the ORM support all possible relations between tables.

See the dedicated article on medium: Database relations with Room

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement