2

我有 2 个实体 Mood,MoodNote,它们都是我从 web 服务中获取的 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;
}

本教程中: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);
4

1 回答 1

1

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

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
            }
        }
    }
});

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

如果我找到更好的解决方案,我会将其发布回此处,如果您有更好的解决方案,请发布作为答案,以便我们学习。

谢谢你。

于 2016-10-24T21:34:40.877 回答