1

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.

1
  • have you found the solution? Jan 16, 2020 at 20:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy