Android - Entity Relationship in Room
Last Updated :
11 May, 2022
This article describes how to define the relationship between entities in the Room persistence library. Because SQLite is a relational database, entities can be related to one another. Entities in Room cannot directly reference other entities because doing so may result in the loading of unnecessary data every time. Even so, you may want to refer to other entities from your entity at times. This can be accomplished in a variety of ways.
Objects Embedded
The @Embedded annotation can be used to represent an object that you want to decompose into subfields within a table (entity). The embedded fields can then be queried in the same way that other individual columns can.
Kotlin
data class GeeksforGeeks(
val course: String?,
val courseName: String?,
val city: String?,
val coursePrice: Int
)
@Entity
data class User(
@PrimaryKey val id: Int,
val firstName: String?,
@Embedded val GeeksforGeeks: GeeksforGeeks?
)
GeekTip#1: If an entity has multiple embedded fields of the same type, you can use the prefix property to make each column unique. The provided value is then appended to the beginning of each column name in the embedded object.
In the preceding example, an object's fields are decomposed into an entity. The @Embedded annotation cannot be used to represent a relationship between multiple entities. You can define the relationship between two entities using either the @Relation annotation or the foreign keys parameter of the @Entity annotation. They differ in that the @Relation annotation can only be applied to non-entity classes, whereas the ForeignKey annotation can only be applied to entity classes. ForeignKey also has an impact on the schema of an entity that requires the child column(s) to exist in the parent column (s). @Relation is used to join tables without affecting their schema.
There are three ways to define the relationship between entities:
- One-on-one interaction
- One-to-many or one-to-many relationship a one-to-many relationship
- Many-to-many connection
One-on-one Interactions
A one-to-one relationship exists when each instance of the parent entity corresponds to exactly one instance of the child entity, and vice versa. Consider a music streaming app in which the user has a library of their own songs. Each user has a single library, and each library corresponds to a single user.
Kotlin
@Entity
data class GfgCourseName(
@PrimaryKey val gfgLoginNumber: Long,
val name: String,
val class: Int
)
@Entity(foreignKeys = @ForeignKey(entity = GfgCourseName.class,
parentColumns = "gfgLoginNumber",
childColumns = "courseActiveID",
onDelete = CASCADE))
data class GfgCourse(
@PrimaryKey val gfgCourseId: Long,
val title: String,
val courseActiveID: Long
)
data class GfgCourseNameAndGfgCourse(
@Embedded val gfgCourseName: GfgCourseName,
@Relation(
parentColumn = "gfgLoginNumber",
entityColumn = "courseActiveID"
)
val gfgCourse: GfgCourse
)
In the preceding example, the entities with a one-to-one relationship are gfgCourseName and coursegfg. One of the entities must include a variable that refers to the other entity's primary key (userOwnerId in gfgCourse entity). To query the list of users and corresponding libraries, we must first model the two entities' one-to-one relationship, which is done using the UserAndLibrary class. The CourseandGfgCourse class contains one instance of the parent entity (gfgCourseName) and one instance of the child entity (gfgCourse).Â
Then, with the parent, add the @Relation annotation to the instance of the child entity. Column set to the name of the parent entity's and entity's primary key column  set to the name of the child entity's column that refers to the primary key of the parent entity.
Relationships that are one-to-many
A one-to-many relationship is one in which each instance of the parent entity corresponds to zero or more instances of the child entity, but each instance of the child entity can only correspond to one instance of the parent entity. A course can have multiple videoPlaylists in the previous courseStructure app example. Each course can create an unlimited number of playlists, but each playlist is created by only one course.
Kotlin
@Entity
data class GfgCourseName(
@PrimaryKey val gfgCourseNameId: Long,
val name: String,
val age: Int
)
@Entity(foreignKeys = @ForeignKey(entity = GfgCourseName.class,
parentColumns = "gfgCourseNameId",
childColumns = "gfgCourseNameCreatorId",
onDelete = CASCADE))
data class Playlist(
@PrimaryKey val courseId: Long,
val gfgCourseNameCreatorId: Long,
val playlistName: String
)
data class GfgCourseNameWithPlaylists(
@Embedded val gfgCourseName: GfgCourseName,
@Relation(
parentColumn = "gfgCourseNameId",
entityColumn = "gfgCourseNameCreatorId"
)
val playlists: List<Playlist>
)
As can be seen, the approach is very similar to a one-on-one relationship; the only difference is in the relationship model (UserWithPlaylists). Instead of a single child entity, it now has a list of child entities. The process of querying the database is also very similar.
Many-to-many connections
A many-to-many relationship exists between two entities when each instance of the parent entity corresponds to zero or more instances of the child entity, and vice versa. In the case of the music streaming app, each playlist can contain many songs, and each song can be part of multiple playlists. As a result, the Playlist entity and the Song entity should have a many-to-many relationship.
Many-to-many relationships differ from other types of relationships in that there is no reference to the parent entity in the child entity. Instead, a third class is used to represent the two entities' associative entity (or cross-reference table). The cross-reference table must include columns for the primary key from each entity in the table's many-to-many relationship.
Kotlin
@Entity
data class CourseVideo(
@PrimaryKey val id: Long,
val courseVideoName: String
)
@Entity
data class CourseNameSignture(
@PrimaryKey val id: Long,
val courseNameSigntureName: String,
val artist: String
)
@Entity(primaryKeys = ["courseVideoId", "courseNameSigntureId"],
foreignKeys = {
@ForeignKey(entity = CourseVideo.class,
parentColumns = "id",
childColumns = "courseVideoId"),
@ForeignKey(entity = CourseNameSignture.class,
parentColumns = "id",
childColumns = "courseNameSigntureId")
}))
data class CourseVideoCourseNameSigntureCrossRef(
val courseVideoId: Long,
val courseNameSigntureId: Long
)
The next step is determined by how you want to query these related entities. Create a new data class that contains a single Playlist object and a list of all of the Song objects that the playlist includes if you want to query playlists and a list of the corresponding songs for each playlist. Create a new data class that contains a single Song object and a list of all the Playlist objects in which the song is included if you want to query songs and a list of the corresponding playlists for each.Â
In either case, model the relationship between the entities by identifying the cross-reference entity providing the relationship between the Playlist entity and the Song entity using the associateBy property in the @Relation annotation in each of these classes.
Kotlin
data class CourseNameSignature(
@Embedded val videoPlaylist: VideoPlaylist,
@Relation(
parentColumn = "videoPlaylistId",
entityColumn = "courseID",
associateBy = @Junction(VideoPlaylistSongCrossRef::class)
)
val songs: List<Song>
)
data class CourseNamePlaylist(
@Embedded val song: Song,
@Relation(
parentColumn = "courseID",
entityColumn = "videoPlaylistId",
associateBy = @Junction(VideoPlaylistSongCrossRef::class)
)
val videoPlaylists: List<VideoPlaylist>
)
The querying of the database is similar to the previous methods.
Kotlin
@Transaction
@Query("SELECT * FROM Playlist")
fun getPlaylistCourse(): List<PlaylistWithCourseNames>
@Transaction
@Query("SELECT * FROM CourseName")
fun getCourseNamesWithPlaylists(): List<CourseNameWithPlaylists>
Similar Reads
How to Draw Entity Relationship Diagrams (ERDs)
Entity Relationship Diagrams serve as valuable tools for designing complex systems and their relationships. This article will go through the step-by-step process of designing an ER diagram and defining how entities, attributes, and relationships are defined. Entity relationship diagrams are importan
6 min read
One-To-Many Relationship Example
One-To-Many Relationship signifies connection between two entities where one entity is associated with multiple instances of the other entity but each instance of second entity is associated with only one instance of first entity. Depending on how we look at it, a one-to-many relationship is also ca
5 min read
How to Express a One-To-Many Relationship in Django?
In Django, expressing relationships between models is crucial to structuring our database. One of the most common relationships is One-To-Many, where a single record in one model is associated with multiple records in another model. For example, an author can write multiple books, but each book is w
4 min read
What is Relationship in DBMS?
A database is a structured data set that is usually electronically written in a computer system and stored as data. With their primary purpose being to maintain, keep, and extract data correctly, databases are important. In many practical situations, data is never an isolated file, and its correlati
5 min read
Introduction to Android Development
Android operating system is the largest installed base among various mobile platforms across the globe. Hundreds of millions of mobile devices are powered by Android in more than 190 countries of the world. It conquered around 71% of the global market share by the end of 2021, and this trend is grow
5 min read
Getting Started with Android Things
Basically, Android Things refers to the summation of Android and the Internet of Things. IoT is very autonomous it means the Internet of Things, whatever the IoT device is, it can sense through sensors and do its respective task without very much human interaction and Android Things revolves around
4 min read
How to Prepopulate Room Database in Android?
Room is one of the Jetpack Architecture Components in Android. This provides an abstract layer over the SQLite Database to save and perform the operations on persistent data locally. This is recommended by Google over SQLite Database although the SQLite APIs are more powerful they are fairly low-lev
3 min read
What is Context in Android?
Android Applications are popular for a long time and it is evolving to a greater level as users' expectations are that they need to view the data that they want in an easier smoother view. Hence, the android developers must know the important terminologies before developing the app. In Android Progr
9 min read
What is Is-A-Relationship in Java?
A relationship in Java means different relations between two or more classes. For example, if a class Bulb inherits another class Device, then we can say that Bulb is having is-a relationship with Device, which implies Bulb is a device. In Java, we have two types of relationship: Is-A relationship:
3 min read
JPA - Inserting an Entity
The JPA (Java Persistence API) is a Java specification for accessing, persisting, and maintaining data between Java objects and a relational database. It can provide a framework for assigning Java objects to database tables and simplify database operations in Java applications. Inserting an entity i
7 min read