【发布时间】:2018-07-16 09:15:03
【问题描述】:
我正在开发一个 Android 应用程序,并在 Android 操作系统中使用新的架构组件:LiveData、ViewModel 和 Room。 我对 Room 实现有一个小问题,即创建一个 @Relation,它从 JOIN 查询(多对多关系)返回结果。
我的数据库结构如下所示:
@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;
}
我如何才能获得仅针对特定学生的所有组,像这样?
public class StudentWithGroups{
@Relation(parentColumn = "id", entityColumn = "rule_id", entity =
StudentGroup.class)
private List<Group> groups;
}
我已经检查过How can I represent a many to many relation with Android Room? 和Android Persistence room: "Cannot figure out how to read this field from a cursor" 之类的问题
【问题讨论】:
标签: android many-to-many android-room android-livedata