使用@Relation注解的房间-一对多关系where子句子关系 [英] Room using the @Relation annotation - one to many relationship where clause child relationship

查看:402
本文介绍了使用@Relation注解的房间-一对多关系where子句子关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用@Relation批注.我可以使用以下查询一对多关系:

Using the @Relation annotation. I can query a one to many relationship using the following:

 @Dao
 public interface PostDao {
        @Query("SELECT * FROM post")
        List<PostWithComments> getPostWithComments();
 }

这是实体

@Entity
public class Post {
    @PrimrayKey
    private int id;
    private String title;
    private String content;
}

@Entity
public class Comment {
    @PrimrayKey
    private int id;
    private int post_id;
    private String content;
    private String status;
}


public class PostWithComments {
    @Embedded
    public Post post;

    @Relation(parentColumn = "id", entityColumn = "post_id", entity = Comment.class)
    public List<Comment> comments;

}

我想获得所有带有status = approved注释的帖子,但是我不确定房间如何处理.我尝试了以下方法:

I would like to get all posts that have a comment with status = approved but I'm not exactly sure how room handles this. I tried the following:

 @Dao
 public interface PostDao {
        @Query("SELECT * FROM post INNER JOIN comment ON post.id = comment.post_id WHERE comment.status = 'approved'")
        List<PostWithComments> getPostWithComments();
 }

结果重复.每个帖子在List<PostWithComments>结果中都有多次.

I got duplicates in the results. Each post is there multiple times in List<PostWithComments> results.

更新:

PostDao_Impl.java处读取生成的代码后,似乎Room正在执行子查询以获取该关系.

After reading the generated code at PostDao_Impl.java it seems that Room is doing a sub query to fetch the relation.

首先,它通过getPostWithComments方法在@Query批注中执行查询,然后为要填充List<Comment>

First, it executes the query in the @Query annotation from the getPostWithComments method, and then it generates a sub query for the relation to populate List<Comment>

SELECT id, post_id, title, content FROM comment WHERE post_id IN (和其他一些逻辑,似乎没有办法修改生成的子查询.

SELECT id, post_id, title, content FROM comment WHERE post_id IN ( and some other logic, and there doesn't seem to be a way to modify the generated sub query.

还有另一种方法吗?

推荐答案

通过@Relation,您可以使用

With @Relation, you can use @DatabaseView

@DatabaseView("SELECT * FROM comments WHERE status = 'approved'")
public class ApprovedComment {
  @Embedded
  Comment comment;
}

PostWithComments类

PostWithComments class

public class PostWithComments {
    @Embedded
    public Post post;

    @Relation(parentColumn = "id", entityColumn = "post_id", entity = ApprovedComment.class)
    public List<ApprovedComment> comments;

}

DAO

@Dao
public interface PostWithCommentsDao {
       @Query("SELECT * FROM post")
       List<PostWithComments> getPostWithComments();
}

您还需要更新扩展RoomDatabase的数据库类,并且可能需要更新版本.

You also need to update your Database class that extends RoomDatabase and you may need to update the version.

@Database(entities = {Post.class, Comment.class}, views = {ApprovedComment.class}, version = 1)
public abstract class MyDatabase extends RoomDatabase

这篇关于使用@Relation注解的房间-一对多关系where子句子关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆