使用多对多和 GreenDao 3.1.0 - Android

Working with Many to Many and GreenDao 3.1.0 - Android

我有 2 个实体 Mood,MoodNote,它们都是我从网络服务中获取的 1 个 json 列表。

Json样本:

{  
  "id":1,
  "name":"Sad",
  "created_at":"2016-08-11 19:59:43",
  "updated_at":"2016-08-16 20:15:17",
  "deleted_at":null,
  "symbol":"\uD83D\uDE22",
  "notes":[
    {  
      "id":409,
      "content":"Recusandae necessitatibus numquam consectetur ut.",
      "created_at":"2016-08-11 20:01:18",
      "updated_at":"2016-08-11 20:01:18",
      "deleted_at":null,
      "count":0,
      "pivot":{  
        "id_mood":1,
        "id_modenote":409,
        "created_at":"2016-08-11 20:01:19",
        "updated_at":"2016-08-11 20:01:19"
      }
    },
    {  
      "id":269,
      "content":"Nulla laudantiums quia impedit.",
      "created_at":"2016-08-11 20:01:18",
      "updated_at":"2016-08-11 20:01:18",
      "deleted_at":null,
      "count":0,
      "pivot":{  
        "id_mood":1,
        "id_modenote":269,
        "created_at":"2016-08-11 20:01:19",
        "updated_at":"2016-08-11 20:01:19"
      }
    },
    {  
      "id":204,
      "content":"Incidunt doloremque",
      "created_at":"2016-08-11 20:01:18",
      "updated_at":"2016-08-11 20:01:18",
      "deleted_at":null,
      "count":0,
      "pivot":{  
        "id_mood":1,
        "id_modenote":204,
        "created_at":"2016-08-11 20:01:19",
        "updated_at":"2016-08-11 20:01:19"
      }
    },
  ]
}

它们之间的关系是多对多的。

现在这是实体 类:

@Entity
public class Mood {
    @Id
    @SerializedName("id")
    @Expose
    private Long id;
    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("created_at")
    @Expose
    private String createdAt;
    @SerializedName("updated_at")
    @Expose
    private String updatedAt;
    @SerializedName("deleted_at")
    @Expose
    private String deletedAt;
    @SerializedName("symbol")
    @Expose
    private String symbol;

    @ToMany
    @JoinEntity(
            entity = JoinMoodNote.class,
            sourceProperty = "moodId",
            targetProperty = "moodNoteId"
    )
    @SerializedName("notes")
    @Expose
    private List<MoodNote> notes = new ArrayList<>();

}

心情笔记:

@Entity
public class MoodNote {
    @Id
    @SerializedName("id")
    @Expose
    private Long id;
    @SerializedName("content")
    @Expose
    private String content;
    @SerializedName("created_at")
    @Expose
    private String createdAt;
    @SerializedName("updated_at")
    @Expose
    private String updatedAt;
    @SerializedName("deleted_at")
    @Expose
    private String deletedAt;
    @SerializedName("count")
    @Expose
    private int count;
}

而在 this tutorial : LINK 中,他们提到要这样做

@Entity
public class JoinMoodNote {
    @Id private Long id;
    private Long moodId;
    private Long moodNoteId;
}

之后他们没有提到如何设置或获取数据

我需要 ManyToMany 的任何示例或它的用法提示。

我尝试使用改造来获得心情,我得到了心情列表

// This only insert the moods, it doesnt insert the MoodNote list
moodDao.insertTx(moods);

我已经通过手动添加记录到数据库解决了这个问题:

moodDao.insertInTx(moodsList); // inserting all moods 
daoSession.runInTx(new Runnable() {
    @Override
    public void run() {
        // Foreach mood get the moodNotesList and add it to database
        for (Mood mood : moodsList) {
            long moodId = mood.getId();
            for (MoodNote note : mood.getMoodNotes()) {
                note.setMoodId(moodId); // nothing but set the moodId of the MoodNote to keep the relation
                moodNoteDao.insert(note); // insert the moodNote to database one by one
            }
        }
    }
});

这不是完美的解决方案,但它解决了我的问题。

如果我找到更好的解决方案,我会post返回这里,如果您有更好的解决方案,请post作为答案,以便我们学习。

谢谢。