我找到了一些示例,这些示例可使用Room实现Android的多对多关系。但是,它们都很简单,涉及仅具有关联实体标识符的关联表,因此仅返回了任一实体的对象列表。如果关联中有一些数据,该如何处理呢?
例如,对于一个科目和学生的关联,如果我想在该关联中有一个“结果”条目,以便每个学生对一个科目都有1个结果,那该怎么做? 实体将是“主题”,“学生”,“结果”,其中结果表提供了关联。 要获得单个主题的所有学生成绩,应该只需要1个join查询,但这是什么,DAO方法应为该类型返回哪种类型?请参阅下面的代码结尾。否则,似乎Room需要两个查询。
@Entity(tableName="students")
public class Student
{
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "studentId")
private int studentId;
@ColumnInfo(name = "studentName")
private String studentName;
}
@Entity(tableName="subjects")
public class Subject
{
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "subjectId")
private int id;
@ColumnInfo(name = "title")
private String title;
}
@Entity(tableName = "results",
primaryKeys = { "subjectId", "studentId" },
foreignKeys = {
@ForeignKey(entity = Subject.class, parentColumns = "subjectId",
childColumns = "subjectId", onDelete = ForeignKey.CASCADE),
@ForeignKey(entity = Student.class, parentColumns = "studentId",
childColumns = "studentId", onDelete = ForeignKey.CASCADE)}
)
public class Result
{
int subjectId;
int studentId;
@ColumnInfo(name = "result")
int result;
}
@Dao
public interface SubjectDAO
{
@Query("SELECT * FROM subjects")
LiveData<List<Subject>> getAllSubjects ();
}
@Dao
public interface StudentDAO
{
@Query("SELECT * FROM students")
LiveData<List<Student>> getAllStudents ();
}
@Dao
public interface ResultDAO
{
@Query("SELECT * FROM students INNER JOIN results ON students.studentId=results.studentId WHERE results.subjectId=:subjectId")
LiveData<List<Student>> getStudentsForSubject (int subjectId);
@Query("SELECT * FROM subjects INNER JOIN results ON subjects.subjectId=results.subjectId WHERE results.studentId=:studentId")
LiveData<List<Subject>> getSubjectsForStudent (int studentId);
/* Only 1 query should be needed, and what is the something returned?
Don't want it to just be "Result"
because that means another query would be used with the same association in order to get the Student data. */
@Query("SELECT ????????????????")
LiveData<List<something>> getStudentResultsForSubject (int subjectId)
}