1 回答

TA贡献1906条经验 获得超10个赞
简而言之,您需要使用关系。例子:
@Entity
public class Comment{
@PrimaryKey
public int id; // comment id
public int postId; // post id this comment belongs to
public String comment;
}
这是带有评论的帖子的 POJO,它将是查询的结果
// Note: No annotation required at this class definition.
public class PostWithComments {
@Embedded
public Post post;
@Relation(parentColumn = "id", entityColumn = "postId", entity = Comment.class)
public List<Comment> comments;
}
您的查询将是:
@Dao
public interface PostCommentsDao {
//Query
@Query("SELECT * FROM Post")
public List<PostWithComments> loadPostWithComments();
}
这将使所有帖子以及属于每个帖子的所有评论都包含在漂亮的 POJO 中。
添加回答
举报