Android:使用领域进行嵌套查询?
我有一个教师列表,每个教师都包含一个学生对象列表。每个学生都有一份他每天必须带的课本清单。看起来是这样的:Android:使用领域进行嵌套查询?,android,sql,realm,realm-migration,realm-list,Android,Sql,Realm,Realm Migration,Realm List,我有一个教师列表,每个教师都包含一个学生对象列表。每个学生都有一份他每天必须带的课本清单。看起来是这样的: Teacher { String teacherName; RealmList<Student> students = new RealmList<>(); } Student { String studentName; RealmList<SchoolDay> schooldays = new RealmList<>(); }
Teacher {
String teacherName;
RealmList<Student> students = new RealmList<>();
}
Student {
String studentName;
RealmList<SchoolDay> schooldays = new RealmList<>();
}
SchoolDay {
String day;
RealmList<RealmString> schoolbooks;
}
这就是我被卡住的地方-我如何子查询这个?要找到我感兴趣的老师:
RealmResults<Teacher> = realm.where(Teacher.class).equalTo("teacherName", "steven").findAll();
RealmResults=realm.where(Teacher.class).equalTo(“teacherName”,“steven”).findAll();
然而,我必须对老师和学生分别运行子查询——或者更好的是,在同一个查询中运行所有查询。我想要得到的最终结果只是代表某个特定学生的教科书的字符串。我怎样才能做到这一点呢?你能这样试一下吗:
realm.where(Teacher.class)
.equalTo("teacherName",teachername)
.equalTo("students.studentName",studentname)
.equalTo("students.schooldays.day",day).findAll();
毕竟您拥有教师对象,并且可以在一个查询中获得变量:
RealmResults<Teacher> teachers= your query above;
for(Teacher teacher:teachers){
//remember still you can have multiple students for given teacher
for(Student student:teacher.getStudents()){
for(Schoolday schoolday:student.getSchooldays()){
schoolday.schoolbooks bla bla bla...
}
}
}
RealmResults teachers=您的上述查询;
适用于(教师:教师){
//请记住,对于给定的老师,您可以有多个学生
for(学生:teacher.getStudents()){
for(Schoolday Schoolday:student.getschoolday()){
学校日,课本,等等。。。
}
}
}
为什么我们使用for循环:因为findAll()
方法可以返回多个结果,如果您希望使用单个教师对象,请使用findFirst()
这将返回给定teachername属性的教师,该属性包含/包括具有给定学生名的学生:studentname
,并且这些学生的上课日具有给定的:day名
我认为最后的解释有点难理解,现在我用例子来解释:
这一问题也有利于参考;这对我很有帮助:你能试一下吗:
realm.where(Teacher.class)
.equalTo("teacherName",teachername)
.equalTo("students.studentName",studentname)
.equalTo("students.schooldays.day",day).findAll();
毕竟您拥有教师对象,并且可以在一个查询中获得变量:
RealmResults<Teacher> teachers= your query above;
for(Teacher teacher:teachers){
//remember still you can have multiple students for given teacher
for(Student student:teacher.getStudents()){
for(Schoolday schoolday:student.getSchooldays()){
schoolday.schoolbooks bla bla bla...
}
}
}
RealmResults teachers=您的上述查询;
适用于(教师:教师){
//请记住,对于给定的老师,您可以有多个学生
for(学生:teacher.getStudents()){
for(Schoolday Schoolday:student.getschoolday()){
学校日,课本,等等。。。
}
}
}
为什么我们使用for循环:因为findAll()
方法可以返回多个结果,如果您希望使用单个教师对象,请使用findFirst()
这将返回给定teachername属性的教师,该属性包含/包括具有给定学生名的学生:studentname
,并且这些学生的上课日具有给定的:day名
我认为最后的解释有点难理解,现在我用例子来解释:
这一问题也有利于参考;这对我很有帮助:我会提出一个更简单的选择 你可以利用 这些模型看起来像:
Teacher {
String teacherName;
RealmList<Student> students = new RealmList<>();
}
Student {
String studentName;
RealmList<SchoolDay> schooldays = new RealmList<>();
@LinkingObjects("students")
final RealmResults<Teacher> teacher = null;
}
SchoolDay {
String day;
RealmList<SchoolBook> schoolbooks;
@LinkingObjects("schooldays")
final RealmResults<Student> student = null;
}
SchoolBook {
String bookName;
@LinkingObjects("schoolbooks")
final RealmResults<SchoolDay> day = null;
}
教师{
字符串教师名称;
RealmList students=新RealmList();
}
学生{
字符串studentName;
RealmList schooldays=新RealmList();
@链接对象(“学生”)
最终RealmResults teacher=null;
}
学校日{
弦日;
RealmList教科书;
@链接对象(“学校日”)
最终RealmResults student=null;
}
教科书{
字符串书名;
@链接对象(“教科书”)
最终RealmResults day=null;
}
查询将非常简单,如下所示:
RealmResults<SchoolBook> = realm
.where(SchoolBook.class)
.equalTo("day.student.studentName", "austin")
.findAll();
RealmResults=realm
.where(课本、班级)
.equalTo(“day.student.studentName”,“austin”)
.findAll();
我会提出一个更简单的选择
你可以利用
这些模型看起来像:
Teacher {
String teacherName;
RealmList<Student> students = new RealmList<>();
}
Student {
String studentName;
RealmList<SchoolDay> schooldays = new RealmList<>();
@LinkingObjects("students")
final RealmResults<Teacher> teacher = null;
}
SchoolDay {
String day;
RealmList<SchoolBook> schoolbooks;
@LinkingObjects("schooldays")
final RealmResults<Student> student = null;
}
SchoolBook {
String bookName;
@LinkingObjects("schoolbooks")
final RealmResults<SchoolDay> day = null;
}
教师{
字符串教师名称;
RealmList students=新RealmList();
}
学生{
字符串studentName;
RealmList schooldays=新RealmList();
@链接对象(“学生”)
最终RealmResults teacher=null;
}
学校日{
弦日;
RealmList教科书;
@链接对象(“学校日”)
最终RealmResults student=null;
}
教科书{
字符串书名;
@链接对象(“教科书”)
最终RealmResults day=null;
}
查询将非常简单,如下所示:
RealmResults<SchoolBook> = realm
.where(SchoolBook.class)
.equalTo("day.student.studentName", "austin")
.findAll();
RealmResults=realm
.where(课本、班级)
.equalTo(“day.student.studentName”,“austin”)
.findAll();
谢谢,但是此方法只返回teacher对象,我想返回包含教科书的字符串。没有办法直接从领域中选择教科书字符串吗?@Jon当你得到老师时,你可以在一个查询中得到他的教科书。如果你不知道怎么做,我可以编辑我的答案。我这样说是因为findAll()
方法可以返回多个具有给定名称的教师,并且您可以使用for loop。您的意思是在多个领域查询中?是的,我将不胜感激。非常感谢。@Jon我已经编辑了我的答案,请看一下,如果你还有问题,请让我听听。所以你的意思是,王国不允许你得到除顶端以外的任何东西