【发布时间】:2018-11-07 03:24:45
【问题描述】:
我有三个表 Notes、Tags 和连接表,外键为 Note 和 Tag,称为 NoteTagJoin,但我怎样才能返回带有所有标签的注释作为一个响应?
这里是获取注释和标签的查询:
SELECT n.*,t.* FROM notes n INNER JOIN note_tag_join nt ON n.entryId = nt.noteId INNER JOIN tags t ON t.tagId = nt.tagId WHERE n.entryId =:noteId
这里是响应类,它必须保存注释和标签列表:
data class NoteResponse(
@Embedded
var note: Note? = null,
@Relation(parentColumn = "entryId", entityColumn = "tagId", entity = Tag::class)
var tags: List<Tag>? = null
)
但是tags list 在响应时是空的,只是有注释,我确定标签和注释存在于 db 中,并且 Join 表具有正确的外键,因为所有其他查询都有效,所以我的猜测是查询是错误的或 @ 987654330@ 类是错误的,因为我使用了不需要的注释 @Relation,但是如果我不在标签上添加 @Relation 注释,则会引发错误,即房间不知道这个列表是什么,所以该怎么做?我找不到任何参考资料,文档只提到在 POJO 中嵌入一个类,但没有列表的示例,所有类似的帖子都只讨论插入列表。
【问题讨论】:
标签: android mysql sqlite android-room