如何使用 Room 从子 table 获取父外键的计数

How to obtain count of parent foreign key from child table using Room

我有一个父实体集合:

@Entity(tableName = "collections")
public class Collection implements Serializable {

    @PrimaryKey
    @NonNull
    public String id;

    public String collTitle;

    public int isFavorite;

    public int wordCount;

    Collection(String collTitle, int isFavorite, int wordCount) {
        this(UUID.randomUUID().toString(), collTitle, isFavorite, wordCount);
    }

    @Ignore
    Collection(@NonNull String id, String collTitle, int isFavorite, int wordCount) {
        this.id = id;
        this.collTitle = collTitle;
        this.isFavorite = isFavorite;
        this.wordCount = wordCount;
    }

}

和一个子实体单词:

@Entity(tableName = "words",
        foreignKeys = @ForeignKey(
                entity = Collection.class,
                parentColumns = "id",
                childColumns = "collId",
                onDelete = SET_DEFAULT),
        indices = @Index("collId"))
public class Word implements Serializable {

    @PrimaryKey
    @NonNull
    public final String id;

    public String wordTitle;

    public String collId;

    public String pageNum;

    public int isFavorite;

    public String wordNotes;

    @Ignore
    Word(String wordTitle, String collId, String pageNum, int isFavorite, String wordNotes) {
        this(UUID.randomUUID().toString(), wordTitle, collId, pageNum, isFavorite, wordNotes);
    }

    Word(@NonNull String id, String wordTitle, String collId, String pageNum, int isFavorite,
         String wordNotes) {
        this.id = id;
        this.wordTitle = wordTitle;
        this.collId = collId;
        this.pageNum = pageNum;
        this.isFavorite = isFavorite;
        this.wordNotes = wordNotes;
    }
}

在插入新集合时,我将 wordCount 设为 0,因为每个新集合都会有 0 个与之关联的单词。但是,当插入一个新的Word时,我必须从定义的集合中选择一个集合并将其'id'用作'collId'(外键)。

现在我需要使用 'wordCount' 字段检索 Collection 对象列表,其中包含与每个 Collection 对象关联的实际单词数。如前所述,'wordCount' 字段在插入 Collection 对象时被赋值 0。

我只是希望我把我的问题说清楚了。请提出一个符合我目的的查询。而且我不想以新的 POJO class 的形式获得结果,例如 CollectionsAndWords.

任何帮助都将是一个很大的帮助!

好的,我是通过以下方式解决的:

在我的 Collection 实体中,我将 wordCount 注释如下:

@ColumnInfo(name = "word_count")
    public int wordCount;

并通过删除此字段对构造函数进行了更改:

Collection(String collTitle, int isFavorite) {
        this(UUID.randomUUID().toString(), collTitle, isFavorite);
    }

@Ignore
Collection(@NonNull String id, String collTitle, int isFavorite) {
        this.id = id;
        this.collTitle = collTitle;
        this.isFavorite = isFavorite;
   }

最后,我在我的 DAO 中添加了以下查询方法:

@Query("SELECT collections.id, collections.collTitle, collections.isFavorite, " +
            "COUNT(words.collId) as word_count " +
            "FROM collections " +
            "LEFT JOIN words " +
            "ON collections.id = words.collId " +
            "GROUP BY collections.id " +
            "ORDER BY collections.collTitle")
List<Collection> findAllCollsWithCount();

以正确的方式使用 LEFT JOIN 为我解决了所有问题。

请注意,在上面的查询中,我没有检索 wordCount 字段,我只是让一个新字段被计算并将其映射到 wordCount。如果我从集合 table 中检索 wordCount 和其他列,我会得到名称为 word_count 的 02 列;一个具有 0 值,另一个具有计算值 (COUNT())。我还尝试使用 @Ignore 注释 wordCount,但随后 Room 找不到任何要映射 word_count 的列。

但是,就目前而言,上述解决方案完全符合我的目的,所以这就是答案。它可能会帮助有同样问题的人。

谢谢!