ActiveAndroid多对多关系

我目前正在使用ActiveAndroid,并且在过去的几个小时里一直试图让多对多的关系工作,但是我无法让它工作。 我希望你能帮助我:

我有模特“学生”和“课程”,学生可以有很多课程,课程有很多学生。 基本上这就是我在模型“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 courses(){ return getMany(Course.class, "StudentCourse"); } public List students(){ return getMany(Student.class, "StudentCourse"); } 

现在我要做的是通过以下代码获得“课程X中的所有学生”:

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

但是我收到以下错误:

java.lang.ClassCastException:com.papinotas.models.StudentCourse无法强制转换为com.papinotas.models.Student

如果我将(学生)的演员表更改为(StudentCourse)我会收到以下错误:

android.database.sqlite.SQLiteException:没有这样的专栏:students.StudentCourse(代码1):,同时编译:SELECT * FROM学生WHERE students.StudentCourse = 1

我的主要目标是希望在1个查询中实现这一点。 任何帮助将不胜感激。 提前致谢!

PS:我已经看过几乎所有我能find的东西: 活跃的Android多对多关系和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(); 

如果我将(学生)的演员改为(学生课程),我会收到以下错误…

演员实际应该是(List) ,但真正的问题在于你的模型的逻辑。 您正在调用executeSingle(),但您确实需要多个StudentCourse对象,以便您获得课程的每个学生 – 课程关系。 您的students()和courses()方法没有多大意义,因为一个StudentCourse对象只有一个学生和一个课程。

相反,我会这样做:

 List enrolments = new Select().from(StudentCourse.class).where("course = ?",selectedCourse.getId()).execute(); List studentsInCourse = new ArrayList(); for(StudentCourse enrolment:enrolments) studentsInCourse.add(enrolment.student); 

像魅力一样工作:

客户类

  @Table(name = "Client") public class Client extends Model{} 

合同类

  @Table(name = "Contract") public class Contract extends Model{} 

客户与合同之间的关系

  @Table(name = "ClientContract") public class ClientContract extends Model { @Column(name = "Client", onDelete = Column.ForeignKeyAction.CASCADE) public Client client; @Column(name = "Contract", onDelete = Column.ForeignKeyAction.CASCADE) public Contract contract; } 

数据库助手

 public class DBHelper { public List getRelatedContracts(Client client) { List contracts = null; if (client != null && client.isCreated()) { contracts = new Select() .from(Contract.class) .innerJoin(ClientContract.class).on("ClientContract.Contract = Contract.id") .where("ClientContract.Client = ?", client.getId()) .execute(); } return contracts; } public List getRelatedClients(Contract contract) { List clients = null; if (contract != null && contract.isCreated()) { clients = new Select() .from(Client.class) .innerJoin(ClientContract.class).on("ClientContract.Client = Client.id") .where("ClientContract.Contract = ?", contract.getId()) .execute(); } return clients; } // get relation public ClientContract getClientContract(Client client, Contract contract) { ClientContract clientContract = null; if (client != null && contract != null && client.isCreated() && contract.isCreated()) { clientContract = new Select().from(ClientContract.class) .where("Client = ?", client.getId()) .where("Contract = ?", contract.getId()) .executeSingle(); } return clientContract; } // add relation public ClientContract addClientContract(Client client, Contract contract) { ClientContract clientContract = getClientContract(client, contract); if (client != null && contract != null && clientContract == null) { if (client.getId() == null) client.save(); if (contract.getId() == null) contract.save(); clientContract = new ClientContract(); clientContract.client = client; clientContract.contract = contract; clientContract.save(); } return clientContract; } // delete relation public void deleteClientContract(Client client, Contract contract) { ClientContract clientContract = getClientContract(client, contract); if (clientContract != null && contract.isCreated()) clientContract.delete(); } }