java - Android Room Persistence Library : What is the best way to implement a many to many relation? -
i used use realm , testing room in order compare both tools.
i trying implement following many many relation :
here entity classes :
the person :
@entity(tablename = "person") public final class roomperson { @primarykey public int id; public string name; } the cat class :
@entity(tablename = "cat") public final class roomcat { @primarykey public int id; public int age; public string name; } and personcat class :
@entity(tablename = "person_cat", primarykeys = { "personid", "catid" }, indices = { @index(value = { "catid" }) }, foreignkeys = { @foreignkey(entity = roomperson.class, parentcolumns = "id", childcolumns = "personid"), @foreignkey(entity = roomcat.class, parentcolumns = "id", childcolumns = "catid") }) public final class roompersoncat { public int personid; public int catid; public roompersoncat(int personid, int catid) { this.personid = personid; this.catid = catid; } } i have pojo in order manipulate person cats app :
public final class roompersonwithanimals { @embedded public roomperson person; @relation(parentcolumn = "id", entitycolumn = "id", entity = roomcat.class) public list<roomcat> cats; } the question : how save list<roompersonwithanimals> ?
should 3 requests each time in order save :
- the person table
person - the cats table
cat - its cats table
personcat
here java code illustrate 3 requests :
for (roompersonwithanimals personwithanimals : persons) { myroomdatabase.roompersondao().insert(personwithanimals.person); myroomdatabase.roomcatdao().insertall(personwithanimals.cats.toarray(new roomcat[personwithanimals.cats.size()])); (roomcat cat : personwithanimals.cats) { myroomdatabase.roompersoncatdao().insert(new roompersoncat(personwithanimals.person.id, cat.id)); } } in realm, it's possible save in 1 request these data. is limitation of room or implementation wrong ?
thank in advance !

Comments
Post a Comment