I am developing an android application and am using the new Architecture Components in Android OS: LiveData, ViewModel and Room. I have a small problem with the Room implementation about creating a @Relation which returns the result from a JOIN query (many-to-many relationship).
My DB structure looks like this:
@Entity
public class Student{
@PrimaryKey
private int id;
private String name;
private String email;
}
@Entity
public class Group{
@PrimaryKey
private int id;
private String name;
}
@Entity(foreignKeys = {
@ForeignKey(entity = Student.class,
parentColumns = "id",
childColumns = "student_id"),
@ForeignKey(entity = Group.class,
parentColumns = "id",
childColumns = "group_id")
})
public class StudentGroup{
private int studentId;
private int groupId;
}
How I can get all Groups only for a specific student, something like this?
public class StudentWithGroups{
@Relation(parentColumn = "id", entityColumn = "rule_id", entity =
StudentGroup.class)
private List<Group> groups;
}
I already checked questions like How can I represent a many to many relation with Android Room? and Android Persistence room: "Cannot figure out how to read this field from a cursor"