我目前正在使用 ActiveAndroid,并且在过去的几个小时里一直试图让多对多关系正常工作,但我就是无法让它正常工作。我希望你能帮助我:
我有“Student”和“Course”两个模型,一个学生可以有很多门类(class),一个类(class)有很多学生。基本上这就是我在“StudentCourse”模型中的内容:
@Column(name = COURSE)
public Course course;
@Column(name = STUDENT)
public Student student;
public StudentCourse(Student student, Course course) {
super();
this.student = student;
this.course = course;
}
//
public StudentCourse(){ }
public List<Course> courses(){
return getMany(Course.class, "StudentCourse");
}
public List<Student> students(){
return getMany(Student.class, "StudentCourse");
}
现在我要做的是使用以下代码获取“类(class) X 中的所有学生”:
((Student) new Select().from(StudentCourse.class).where("course = ?",selectedCourse.getId()).executeSingle()).students();
但是我得到以下错误:
java.lang.ClassCastException: com.papinotas.models.StudentCourse 无法转换为 com.papinotas.models.Student
如果我将 (Student) 的类型转换更改为 (StudentCourse),我会收到以下错误:
android.database.sqlite.SQLiteException: no such column: students.StudentCourse (code 1): , 编译时:SELECT * FROM students WHERE students.StudentCourse=1
我的主要目标是希望在 1 个查询中实现这一目标。任何帮助将不胜感激。提前致谢!
PS:我已经查看了几乎所有我能找到的东西:Active Android many-to-many relationship和 https://github.com/pardom/ActiveAndroid/issues/46
最佳答案
我不会为此使用 getMany
。我会这样做:
return new Select()
.from(Student.class)
.innerJoin(StudentCourse.class).on("students.id = studentcourses.id")
.where("studentcourses.course = ?", courseId)
.execute();
关于java - ActiveAndroid 多对多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26207948/