在房间数据库中使用相同的 class 创建两个表

create two tables using the same class in room database

我使用 Room 作为应用程序的数据库。我正在使用 Retrofit 从服务器获取数据。场景是我有一个名为 Photo 并用 Entity 注释的 class 供房间使用,我正在使用这个 class 使用 retroft 映射 API 的响应。我需要使用相同的 class 创建两个 table,例如:latest_photos table 和 popular_photos table。我怎样才能做到这一点。

我在这里插入照片列表(由 20 个照片对象组成)。我怎样才能为每个照片对象添加一个新字段。 我试过使用 for 循环,但它需要很长的时间,因为我只收到 20 件商品,但我正在通过网络服务器进行分页。

我已经解决了这个问题,现在我只处理一个 table。我已经插入了一个新列来指示天气照片是热门的还是最新的,但问题是服务器没有 returning 任何有用的数据来填充此列。所以唯一的解决方案是在映射到我的实体之前修改 json 响应。所以使改造调用 returning 类型成为 ResponseBody 到 return json 字符串本身而不是映射它,所以我可以添加一个 属性 到 json 响应然后我将其映射到新添加的 属性.

  private void processData(Response<ResponseBody> response) {
        if (response.isSuccessful()) {
            try {

                JSONObject jsonObject = new JSONObject(response.body().string());
                JSONArray hits = jsonObject.getJSONArray("hits");
                List<Photo> photos = new ArrayList<>();
                for (int i = 0; i < hits.length(); i++) {
                    JSONObject photoJSONObject = hits.getJSONObject(i);

                    photoJSONObject.put("order", order); // add new property

                    String json = photoJSONObject.toString();

                    Gson gson = new Gson();

                    Photo photo = gson.fromJson(json, Photo.class);

                    photos.add(photo);


                }