我有 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);