【问题标题】:Room database many to many relation get from couple tables one response房间数据库多对多关系从一对表中得到一个响应
【发布时间】:2018-11-07 03:24:45
【问题描述】:

我有三个表 NotesTags 和连接表,外键为 NoteTag,称为 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


【解决方案1】:

请看我实现的多对多关系CinemaActorJoinDao

你可以用我的代码代替你的,如果你有任何问题,我会试着问你:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-22
    • 2019-04-17
    • 1970-01-01
    • 1970-01-01
    • 2020-03-10
    • 2018-06-13
    相关资源
    最近更新 更多