java - ActiveAndroid 多对多关系

标签 java android sql sqlite activeandroid

我目前正在使用 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 relationshiphttps://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/

相关文章:

java - 为什么 Play Framework war 包含重复的类文件?

java - SpringMVC 模型属性在发布时返回 null

android - ExoPlayer发布后如何重新初始化?

MySQL查询问题

SQL分区消除

java - 如何在单个打印中打印两个 webelement?

java - Netty:如何在没有任何 header /字段指示长度的情况下在服务器上接收可变长度消息

java - Android 运行时权限对话框未显示

android - 三星 Galaxy S7 (Camera2) 上的 YUV_420_888 解读

SQL查询以计算每月总计作为一列