Blog Infos
Author
Published
Topics
, , , ,
Published

Relationships are essential concept in projecting and implementing your database. Learn how to define and use one-to-one, one-to-many and many-to-many relationships in Android Room.

For those who want to jump into the code, check the data package inside:

https://github.com/AndroBrain/RoomRelationshipsExample?source=post_page—–729d3c705fd9——————————–

1. One-To-One Relationship
One-To-One
@Entity
data class UserEntity(
    @PrimaryKey val id: Long,
    val name: String,
    val surname: String,
)

@Entity
data class LibraryEntity(
    @PrimaryKey val id: Long,
    val ownerId: Long,
)

Now that we have defined the entities, how do we query them together? You must create a new class that embeds the User and uses the relation between the Library and the User.

data class UserWithLibrary(
    @Embedded val user: UserEntity,
    @Relation(
        parentColumn = "id",
        entityColumn = "ownerId",
    )
    val library: LibraryEntity,
)

Then, in your DAO, you can query it together:

@Transaction
@Query("SELECT * FROM UserEntity")
suspend fun getUsersWithLibraries(): List<UserWithLibrary>

Querying a class with Relation fields requires you to use @Transaction.

2. One-To-Many Relationship
One-To-Many

Now, let’s add the Collections so the Users can create them inside their Library.

@Entity
data class CollectionEntity(
    @PrimaryKey val id: Long,
    val name: String,
    val libraryId: Long,
)

Similar to a one-to-one relationship, we need to create a class with an @Relation, but its field will be a list.

data class LibraryWithCollections(
    @Embedded val library: LibraryEntity,
    @Relation(
        parentColumn = "id",
        entityColumn = "libraryId"
    )
    val collections: List<CollectionEntity>,
)

Then, in your DAO, query it together with libraries:

@Transaction
@Query("SELECT * FROM LibraryEntity")
suspend fun getLibrariesWithCollections(): List<LibraryWithCollections>

OUR VIDEO RECOMMENDATION

, ,

Migrating to Jetpack Compose – an interop love story

Most of you are familiar with Jetpack Compose and its benefits. If you’re able to start anew and create a Compose-only app, you’re on the right track. But this talk might not be for you…
Watch Video

Migrating to Jetpack Compose - an interop love story

Simona Milanovic
Android DevRel Engineer for Jetpack Compose
Google

Migrating to Jetpack Compose - an interop love story

Simona Milanovic
Android DevRel Engin ...
Google

Migrating to Jetpack Compose - an interop love story

Simona Milanovic
Android DevRel Engineer f ...
Google

Jobs

No results found.

3. Many-To-Many Relationship

Now, we want to allow Users to add Games to their Collections and let other Users browse Collections containing the Game to find new exciting games. To make many-to-many relationships possible, we need a cross-reference table.

@Entity
data class GameEntity(
    @PrimaryKey val id: Long,
    val name: String,
)

@Entity
data class CollectionGameCrossRef(
    @PrimaryKey val collectionId: Long,
    @PrimaryKey val gameId: Long,
)

Now, there’ll be two classes using the relation:

data class GameWithCollections(
    @Embedded val game: GameEntity,
    @Relation(
        parentColumn = "id",
        entityColumn = "id",
        associateBy = Junction(CollectionGameCrossRef::class)
    )
    val collection: List<CollectionEntity>,
)

data class CollectionWithGames(
    @Embedded val collection: CollectionEntity,
    @Relation(
        parentColumn = "id",
        entityColumn = "id",
        associateBy = Junction(CollectionGameCrossRef::class)
    )
    val games: List<GameEntity>,
)

They’re very similar because we named the primary key inside GameEntity and CollectionEntity with the same name id. Then you’re able to query it inside your DAO:

@Transaction
@Query("SELECT * FROM CollectionEntity")
fun getCollectionsWithGames(): List<CollectionWithGames>

@Transaction
@Query("SELECT * FROM GameEntity")
fun getGamesWithCollections(): List<GameWithCollections>

Congratulations! Now you know how to define and use all possible relationships between data in Room! I’ll be grateful for a 👏 if you like it!

Check out the GitHub code:

https://github.com/AndroBrain/RoomRelationshipsExample?source=post_page—–729d3c705fd9——————————–

References:

https://developer.android.com/training/data-storage/room/relationships?source=post_page—–729d3c705fd9——————————–

This blog is previously published on proandroiddev.com

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
Using annotations in Kotlin has some nuances that are useful to know
READ MORE
blog
One of the latest trends in UI design is blurring the background content behind the foreground elements. This creates a sense of depth, transparency, and focus,…
READ MORE
blog
The ModalBottomSheet in Jetpack Compose is easy to use, it just pops up at…
READ MORE
blog
Discussions about accessibility, especially in software development, often center around screen reader accessibility. With…
READ MORE

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

Menu