I have 2 entity models in Android application and I use Room Persistence Library for database operations.
User model:
@Entity(tableName = "users")
data class User(
val id: Int,
val name: String
)
Post model:
@Entity(
tableName = "posts",
foreignKeys = [
ForeignKey(
entity = User::class,
parentColumns = ["id"],
childColumns = ["userId"]
)
]
)
data class Post(
val id: Int,
val content: String,
val userId: Int//,
// val user: User
)
I know how to load a user with all his/her posts. But I need to load single Post with its User, so I can access user of the post by typing
val post: Post = db.postDao().find(postId)
val user: User = post.user
How can I achieve this?
Thank you in advance.