在房间数据库中存储带有列表的模型对象

Storing a model object with Lists in Room Database

看了各种在线资源,关于这个话题,我似乎无法弄清楚。

我有一个如下所示的模型对象:HomeResponse

@Entity(tableName = "HomeData")
public class HomeResponse {

  @PrimaryKey
  public  int counts;

  public List<Trade> mTrades;
}

我的 Trades 对象如下所示:

@Entity
public class Trade {
  public int tradeId;
  public String title;
}

我首先看到的是 @TypeConverter 然而,这似乎只适用于只包含原始字段的 POJO。

看来,我要找的应该是@ForeignKey

感觉还是不太对。因为我的 Trades 对象不会只出现在 HomeResponse 对象中。我可能在与不同 Tades 相关的其他 POLO 中有它。

HomeResponse 也将 Trades 对象也存储在数据库中的理想方法是什么?

I have a model object that looks like this: HomeResponse

那不是suitable房间@Entity。删除 public List<Trade> mTrades 字段。

It seems then, what I am looking for would be a @ForeignKey

正确,在 Trade,指向 HomeResponse

This still doesn't feel right. As my Trades object won't only ever be in a HomeResponse object. I may have it in other POLOs that relate to different Tades.

我不确定 POLO 是什么。无论如何,实体充当数据库 table 的模型。将 Room 搁置几分钟,然后设计您的 table 结构。然后,创建反映 table 结构的实体 类。

What would be the ideal way to have HomeResponse also store the Trades object in the DB as well?

没有。您的 DAO 保存 HomeResponse 个对象和 Trade 个对象。