I have 2 Entities Mood, MoodNote, both of them I get them from webservice as 1 json list.
Json sample:
{
"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"
}
},
]
}
The relation between them is many to many.
now this is the entities classes:
@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<>();
}
The mood note:
@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;
}
And in this tutorial : LINK , they mentioned to do like this
@Entity
public class JoinMoodNote {
@Id private Long id;
private Long moodId;
private Long moodNoteId;
}
they did not mention after that how to set or get data
I need any example of ManyToMany or a hint of its usage.
I tried to get the Mood using retrofit and I got list of moods
// This only insert the moods, it doesnt insert the MoodNote list
moodDao.insertTx(moods);
I have solved the problem by adding the records to the database manually:
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
}
}
}
});
This is not the perfect solution, but it solved my problem.
If i find a better solution i will post it back here, if you have a better solution please post is as answer so we learn it.
Thank you.