Sign in Go Pro

Storing Data in Android with Room

Many to Many Relationship

This lesson is for PRO members.

Upgrade today to get access to all the PRO lessons.

Unlock this lesson

Up next




In this lesson, we'll discuss what a many-to-many relationship is, and show how we can model a many-to-many relationship using Room.

We'll create a many-to-many relationship between Users and Tasks. We'll walk through all of the steps required to create the entity and queries for adding, removing and querying a many-to-many relationship in the database.

Summary of Content:

  • What a many-to-many relationship is and how they work
  • What SQL join table is and why we need it
  • Creating an entity for the join table
  • Which primary and foreign keys we'll need for the join table entity
  • How to insert into the join table, so that we can assign a task to a user
  • Use of OnConflictStrategy to prevent adding duplicate data
  • How to query for all tasks, which also retrieves all of the assigned users
  • How to query for all users, which also retrieves all of the assigned tasks
  • How to join across multiple tables to retrieve all the data required
  • How to model the types that will be retrieved from the database, using @Embedded annotation
  • How to transform the return type into something easier to work with
  • How to convert null relations into an empty list so we don't have to think about nullability





There are no comments on this lesson, start the conversation below ...


Lessons in Storing Data in Android with Room