Hi friends! It has been a long time since my last post. I’ve been really tired for the past 6 months. Let’s get to the point then.
On the previous post, we already discussed the basic Room from AAC (Android Architecture Component). In this post, we’re gonna explore the how to create a relationship between tables
So in the previous post, we have a
User table with a schema like this:
|User ID||Int, Generated, PK|
We want to create a relationship so we need another table. Let’s make
|Car Id||Int, Generated, PK|
|Owner||Int, FK to User|
The relation will be one-to-many where one user can own many cars.
Our goals are:
Implementing relationship in Room is very easy, you just have to define the relationship in your
@Entity. In our case, we have to define that
Car table is an FK to
userId in the
@Entity( tableName = "car", foreignKeys = [ForeignKey( entity = User::class, parentColumns = ["userId"], childColumns = ["owner"] )] ) data class Car( @PrimaryKey(autoGenerate = true) val carId: Int? = null, val name: String, val owner: Int )
Here you can see, we define our relationship in between
Car @Entity. Let me make a brief explanation:
entityis a target class, in this case, is our
parentColumnsis our target column, it has to be a primary key. In our case,
childColumnsis our foreign key defined where this rule is written. In our case,
OK, done. Let’s create some query!
Our first goal is that we can have all the user that owns certain Car. For this, we can simply create a query in our DAO.
@Query("SELECT u.* FROM user u JOIN car c WHERE c.name = :name") fun getUsersWithCar(name: String): List<User>
Hooray! We already create our first join query in Room.
Our second goal is that we can query all the users with all their cars.
We want to make the data representation for this query to be separated.
So we create our
UserWithCars class that has a content:
data class UserWitCars( @Embedded var user: User? = null, @Relation(parentColumn = "userId", entityColumn = "owner") var cars: List<Car>? = null )
Simple right? Here we define our relationship for
parentColumnis our target key in
entityColumnis our FK in defined list entity
Then we just create or simple query in
@Query("SELECT * FROM user") fun getUserWithCars(): List<UserWitCars>
Done! We just querying all our users will all their cars!
You can find the source of this example on my Github
For the next article, we’re gonna discuss about creating a transaction in Room. It will be a quick one though.
That’s all for now, see you guys later. Cao 👋