我目前正在使用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<Course> courses(){
return getMany(Course.class, "StudentCourse");
}
public List<Student> 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
我的主要目标是希望在一次查询中实现这一点。任何帮助将不胜感激。提前谢谢!
PS:我已经看过几乎所有我能找到的东西:Active Android many-to-many relationship和https://github.com/pardom/ActiveAndroid/issues/46
答案 0 :(得分:12)
我不会使用getMany
。我这样做了:
return new Select()
.from(Student.class)
.innerJoin(StudentCourse.class).on("students.id = studentcourses.id")
.where("studentcourses.course = ?", courseId)
.execute();
答案 1 :(得分:1)
如果我将(学生)的演员阵容改为(学生课程),我会收到以下错误......
演员实际应该是(List<StudentCourse>)
,但真正的问题在于你的模型的逻辑。您正在调用executeSingle(),但您确实需要多个StudentCourse对象,以便您获得课程的每个学生 - 课程关系。你的students()和courses()方法没有多大意义,因为一个StudentCourse对象只有一个学生和一个课程。
我会这样做:
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);
答案 2 :(得分:1)
像魅力一样工作:
客户端类
@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<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();
}
}