我正在尝试从Room数据库检索完整模型,但是没有成功。
我的数据库设置如下:
@Entity(tableName = "Message",
foreignKeys = [ForeignKey(entity = User::class, parentColumns = arrayOf("id"), childColumns = arrayOf("userId"))])
data class Message(
@PrimaryKey
var id: Long = 9L,
var userId: Long = 0L,
var content: String = "",
)
@Entity(tableName = "Attachment",
foreignKeys = [ForeignKey(entity = Message::class, parentColumns = arrayOf("id"), childColumns = arrayOf("messageId"))])
data class Attachment(
@PrimaryKey
var id: String = "",
var messageId: Long = 0L,
var url: String = ""
)
@Entity(tableName = "User")
data class User(
@PrimaryKey
var id: Long = 0L,
var name: String = "",
var avatarId: String = ""
)
数据库关系: -一封邮件属于一位用户,一封邮件具有多个附件
我现在希望能够直接从数据库LiveData<List<FullMessage>>
中检索,其中:
class FullMessage(
val id: Int,
val content: String,
val user: User,
val attachments: List<Attachment>
)
在道方面,我知道我能够做到:
@Query("SELECT * from Message")
fun getAll(): LiveData<List<FullMessage>>
我现在在FullMessage类中缺少使用Room中的@Relation批注。如何检索模型FullMessage?