我在Room DAO中有一个查询,该查询(针对提供的countryId
)查询我的share_groups
表,并返回包含num_twitter_users
数字(通过计算数字来计算)的行share_group_individuals
表中填充的行数)。
这是查询的代码:
@Query("SELECT `share_groups`.`_id`, `share_group_name`, `share_group_description`, `share_group_instructions`, `share_group_order`, `share_group_is_visible`, `share_groups`.`last_modified`" +
// `num_twitter_users` will contain the number of share_group_individuals within this share_group that have a Twitter username.
", COUNT(DISTINCT(CASE WHEN (`share_group_individuals`.`share_group_individual_twitter` IS NOT NULL AND `share_group_individuals`.`share_group_individual_twitter` != '') THEN `share_group_individuals`.`_id` ELSE NULL END)) AS `num_twitter_users`" +
" FROM `share_groups`" +
// Join with `share_group_individuals` - on countryId
" LEFT JOIN `share_group_individuals` ON (`share_group_individuals`.`share_group_id` = `share_groups`.`_id`" +
" AND `share_group_individuals`.`share_group_individual_country_id` = :countryId)" +
// Search on countryId
" WHERE `share_group_individuals`.`share_group_individual_country_id` = :countryId" +
" GROUP BY `share_groups`.`share_group_name`, `share_group_individuals`.`share_group_id`" +
// Only return share_groups that contain at least 1 share_group_individual with a Twitter account
" HAVING `num_twitter_users` > 0" +
" ORDER BY `share_group_order` ASC")
LiveData<List<ShareGroup>> getAllShareGroups(long countryId);
但是,我无法从返回的num_twitter_users
中访问LivaData<List<ShareGroup>>
的值。
我想知道解决方案的一部分是否可能是创建一个ShareGroupWithData
POJO(并因此从我的查询中返回一个LivaData<List<ShareGroupWithData>>
),但是,如果这是正确的方法,我将如何填充numTwitterUsers
类中的ShareGroupWithData
变量?
我在网上或official documentation上都找不到任何信息,因此朝正确的方向和/或一些示例代码表示感谢。
为完整起见,此实体代表的我的share_groups
表:
@Entity(tableName = "share_groups")
public class ShareGroup {
@PrimaryKey
@ColumnInfo(name = "_id")
private long id;
@NonNull
@ColumnInfo(name = "share_group_name")
private String shareGroupName;
@ColumnInfo(name = "share_group_description")
private String shareGroupDescription;
@ColumnInfo(name = "share_group_instructions")
private String shareGroupInstructions;
@ColumnInfo(name = "share_group_order", defaultValue = "10")
private int shareGroupOrder;
@ColumnInfo(name = "share_group_is_visible", defaultValue = "1")
private boolean shareGroupIsVisible;
@ColumnInfo(name = "last_modified")
private long lastModified;
...
}
我的share_groups_individual
表由该实体表示:
@Entity(tableName = "share_group_individuals",
indices = {@Index(name = "index_share_group_id", value = "share_group_id")},
foreignKeys = @ForeignKey(entity = ShareGroup.class, parentColumns = "_id", childColumns = "share_group_id", onDelete = ForeignKey.CASCADE)
)
public class ShareGroupIndividual {
@PrimaryKey
@ColumnInfo(name = "_id")
private long id;
@ColumnInfo(name = "share_group_id")
private long shareGroupId;
@ColumnInfo(name = "share_group_individual_country_id", defaultValue = "-1")
private long shareGroupIndividualCountryId;
@ColumnInfo(name = "share_group_individual_name")
private String shareGroupIndividualName;
@ColumnInfo(name = "share_group_individual_twitter")
private String shareGroupIndividualTwitter;
...
}
答案 0 :(得分:0)
最后,我通过创建这样的POJO来解决了这个问题:
public class ShareGroupWithData {
@Embedded
public ShareGroup shareGroup;
@ColumnInfo(name = "num_twitter_users", defaultValue = "0")
public int numTwitterUsers;
...
}
我的DAO代码更改为:
LiveData<List<ShareGroupWithData>> getAllShareGroupsWithData(long countryId);