Android Room: How to model relationships?

27,144

You can use @Relation annotation to handle relations at Room.

A convenience annotation which can be used in a Pojo to automatically fetch relation entities. When the Pojo is returned from a query, all of its relations are also fetched by Room.

See document.

(Google's document has confusing examples. I have written the steps and some basic explanation at my another answer. You can check it out)

Share:
27,144

Related videos on Youtube

Rene Ferrari
Author by

Rene Ferrari

Hi! Ever since I came in touch with developing Android Apps (which was Android Studio 0.8.13) through a school project I had fallen in love with it. From that point on I knew that I want to turn this passion into my job - fortunately I was able to do that :). Overall I have 6 years of experience in Android development, 4 of them professionally. Furthermore I have 2 years of professional experience in iOS development.

Updated on September 10, 2020

Comments

  • Rene Ferrari
    Rene Ferrari over 3 years

    I have just started working with Room and although everything seems to be pretty intuitive I currently don't really understand how exactly I could handle relationships.

    Because SQLite is a relational database, you can specify relationships between objects. Even though most ORM libraries allow entity objects to reference each other, Room explicitly forbids this. Even though you cannot use direct relationships, Room still allows you to define Foreign Key constraints between entities.(Source: https://developer.android.com/topic/libraries/architecture/room.html#no-object-references)

    1. How should you model a Many to Many or One to Many Relationship?
    2. What would this look like in practice (example DAOs + Entities)?
    • CommonsWare
      CommonsWare about 7 years
      With Room, rather than the POJOs modeling database tables, the POJOs model query result sets. This is akin to Web service API clients (e.g., Retrofit, Apollo-Android), which do not model the underlying Web service's database, but rather model the results that you are querying. Hence, with Room, there are no relations, because a query result does not have a relation.
    • Rene Ferrari
      Rene Ferrari about 7 years
      So basically a "regular query result" is then converted to a POJO? And therefore there is no relationship on a POJO level? Did I understand it correctly? And thanks for your comment!
    • CommonsWare
      CommonsWare about 7 years
      "Did I understand it correctly?" -- yes, that is how I am interpreting Room. They cover this point in the Google I|O 2017 presentation on Room, towards the end.
    • Rene Ferrari
      Rene Ferrari about 7 years
      Again thank you! By the way amazing book - must check it out some time!
  • Rene Ferrari
    Rene Ferrari almost 7 years
    Thanks that really helps a lot! When they presented Room at the Google I/O I thought everything is gonna be really simple. Now after seeing how Relationships are handled I think it will confuse you very easily since so many classes have to be created (confusion will probably start if you have 5+ tables and many relationships between them haha).
  • Benoit Duffez
    Benoit Duffez almost 7 years
    @ReneFerrari: I think the complexity is in the code in order to have better performance and no landmines when running the app. Lazy loading is great for coding but it is bad because you never know in which thread the database will get queried. You can skip frames or worse get ANRs just by lazy loading a field in a list view.
  • julienduchow
    julienduchow about 5 years
    How can you handle the following with room: I have a Player, Game and a PlayerInGame entity. Now I want to load the Game containing a list of PlayerInGame objects and each PlayerInGame object should also contain the Player. Is that possible with room?