简体   繁体   English

ActiveAndroid多对多的关系

[英]ActiveAndroid Many-to-many relationship

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. 我目前正在使用ActiveAndroid,过去几个小时一直试图让多对多的关系工作,但是我无法让它工作。 I hope you can help me out: 我希望你能帮助我:

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”: 基本上这就是我在模型“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");
 }

Right now what I'm trying to do is get “all students in course X”,with the following code: 现在我正在尝试做的是“课程X中的所有学生”,使用以下代码:

((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 cannot be cast to com.papinotas.models.Student java.lang.ClassCastException:com.papinotas.models.StudentCourse无法强制转换为com.papinotas.models.Student

If I change the cast of (Student) to (StudentCourse) I get the following error: 如果我将(学生)的演员表更改为(StudentCourse),我会收到以下错误:

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

My main goal is to hopefully achieve this in just 1 query. 我的主要目标是希望在一个查询中实现这一点。 Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks in advance! 提前致谢!

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 PS:我已经看过几乎所有我能找到的东西: 活跃的Android多对多关系https://github.com/pardom/ActiveAndroid/issues/46

I wouldn't use getMany for this. 我不会为此使用getMany I'd do this instead: 我会这样做:

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

Work like a charm: 像魅力一样工作:

Client class 客户类

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

Contract class 合同类

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

Relation between Client and Contract 客户与合同之间的关系

 @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;
}

Database helper 数据库助手

public class DBHelper {

 public List<Contract> getRelatedContracts(Client client) {
        List<Contract> 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<Client> getRelatedClients(Contract contract) {
        List<Client> 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();
    }
}

If I change the cast of (Student) to (StudentCourse) I get the following error... 如果我将(学生)的演员改为(学生课程),我会收到以下错误...

The cast should actually be to (List<StudentCourse>) , but the real problem is in the logic of your model here. 演员应该实际上是(List<StudentCourse>) ,但真正的问题在于你的模型的逻辑。 You are calling executeSingle(), but you really want multiple StudentCourse objects so that you get every Student-Course relationship for the Course. 您正在调用executeSingle(),但您确实需要多个StudentCourse对象,以便您获得课程的每个学生 - 课程关系。 Your students() and courses() methods don't make much sense, since one StudentCourse object only has one Student and one Course. 您的students()和courses()方法没有多大意义,因为一个StudentCourse对象只有一个学生和一个课程。

I would do it like so instead: 相反,我会这样做:

List<StudentCourse> enrolments = new Select().from(StudentCourse.class).where("course = ?",selectedCourse.getId()).execute();

List<Student> studentsInCourse = new ArrayList<Student>();
for(StudentCourse enrolment:enrolments)
    studentsInCourse.add(enrolment.student);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM