如何在房间数据库中加入多个 table

How to join multiple table in Room Database

我有一个像这样的摘要class:

public abstract class NotificationParent {
  @SerializedName(SerCons.C_NOTIFICATION_ID) private int notificationId;
  @SerializedName(SerCons.C_ID) private int id;
  @SerializedName(SerCons.C_DATE) private long date;
  ...
}

和两个 class 继承自上述摘要 class:

public class NotifNewRingTone extends NotificationParent {
  @SerializedName(SerCons.C_GAME_ID) private int rtId;
  @SerializedName(SerCons.C_GAME_NAME) private String rtName;
..
}
public class NotifNewWallpaper extends NotificationParent {
  @SerializedName(SerCons.C_GAME_ID) private int wpId;
  @SerializedName(SerCons.C_GAME_NAME) private String wpName;
  ...
}

我想在数据库中创建 3 个表:notificationswallpaper_notificationringtone_notifications。 但是当我想select(加入)从表格中获取所有通知(壁纸和铃声)时,我不知道我需要做什么。

我想要这样的东西:

@Query("select * from notifications n left join ringtone_notifications r on n.notif_id = r.notif_id left join wallpaper_notifications w on n.notif_id = w.notif_id) public NotificationParent getAllNotifications();

但它只会给我 NotificationParent class 包含的字段。不是 subclasses 的字段。 另外,我希望查询准备好实施 PagedList 架构。

即使我必须更改 classes 的设计,也请提出您的建议。

您的 select 查询看起来正确。但是你的方法不对。

public NotificationParent getAllNotifications();

这意味着你想要return有Notificationparent.You的字段需要再做一个class,例如:

public class MyObject {
    private int notificationId;
    private int id;
    private long date;
    private int rtId;
    private String rtName;
    private int wpId;
    private String wpName;
 }

把你桌子上的所有东西都放进去。如果你不想要你需要的一切,你需要修改你的 select * 你可以把

Select notifications.date, ringtone_notifications.rtName

你的对象会是这样的:

public class MyObject {
        private long date;
        private String rtName;
     }