ActiveAndroid许多一对多的关系 [英] ActiveAndroid Many-to-many relationship

查看:825
本文介绍了ActiveAndroid许多一对多的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用ActiveAndroid,并已试图获得许多一对多的关系,在过去的几个小时的工作,但我只是不能得到它的工作。我希望你能帮助我:

I’m currently using ActiveAndroid, and have been trying to get a many-to-many relationship to work for the past few hours, however I just can’t get it to work. I hope you can help me out:

我有机型学生和课程,学生可以有很多课程,课程有很多学生。基本上这是我的模式StudentCourse

I have the models "Student" and "Course", a student can have many courses, and a course has many students. Basically this is what I have in the model "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");
 }

现在我想要做的就是所有的学生在课程X,用下面的code:

Right now what I’m trying to do is get "all students in course X",with the following code:

((Student) new Select().from(StudentCourse.class).where("course = ?",selectedCourse.getId()).executeSingle()).students();

不过,我得到以下错误:

However I get the following error:

java.lang.ClassCastException:com.papinotas.models.StudentCourse不能转换为com.papinotas.models.Student

java.lang.ClassCastException: com.papinotas.models.StudentCourse cannot be cast to com.papinotas.models.Student

如果我改变的(学生)投来(StudentCourse)我收到以下错误:

If I change the cast of (Student) to (StudentCourse) I get the following error:

android.database.sqlite.SQLiteException:没有这样的列:students.StudentCourse(code 1),在编译:SELECT * FROM学生WHERE students.StudentCourse = 1

android.database.sqlite.SQLiteException: no such column: students.StudentCourse (code 1): , while compiling: SELECT * FROM students WHERE students.StudentCourse=1

我的主要目标是在短短1查询有望实现这一目标。任何帮助将大大AP preciated。在此先感谢!

My main goal is to hopefully achieve this in just 1 query. Any help would be greatly appreciated. Thanks in advance!

PS:我已经看了pretty太多的一切,我能找到的:的 Android活跃许多一对多的关系 https://github.com/pardom / ActiveAndroid /问题/ 46

PS: I've already looked at pretty much everything I could find: Active Android many-to-many relationship and https://github.com/pardom/ActiveAndroid/issues/46

推荐答案

我不会用 getMany 这一点。我应该这样做,而不是:

I wouldn't use getMany for this. I'd do this instead:

return new Select()
    .from(Student.class)
    .innerJoin(StudentCourse.class).on("students.id = studentcourses.id")
    .where("studentcourses.course = ?", courseId)
    .execute();

这篇关于ActiveAndroid许多一对多的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆