我是 Greendao 的新手。我正在编写一个用于在 greendao 中生成实体的生成器。所以,我有两个实体医院和病人。他们之间有一对多的关系。所以,一个医院可以有很多病人,但一个病人只能有一个医院。现在Property hospitalId = patient.addLongProperty("hospitalId").getProperty();
这会在患者表中添加一个 hospitalid 列。和
ToMany hospitalToPatients = hospital.addToMany(patient, hospitalId);
这条线在医院和病人之间建立了一对多的关系。那么这些线有什么用 patient.addToOne(hospital, hospitalId);
以及hospitalToPatients.setName("patients");
如何在greendao中实现一对一、一对多、多对一和多对多的关系?PS:我复制了这段代码http://www.vertabelo.com/blog/technical-articles/a-comparison-of-android-orms
public class ProjectGenerator {
public static void main(String[] args) throws Exception {
Schema schema = new Schema(1, "com.example.project");
// hospital table
Entity hospital = schema.addEntity("Hospital");
hospital.addIdProperty();
hospital.addStringProperty("name");
// patient table
Entity patient = schema.addEntity("Patient");
patient.addIdProperty();
patient.addStringProperty("name");
Property hospitalId = patient.addLongProperty("hospitalId").getProperty();
// patient has a one assigned hospital
patient.addToOne(hospital, hospitalId);
// hospital has many patients
ToMany hospitalToPatients = hospital.addToMany(patient, hospitalId);
hospitalToPatients.setName("patients");
// trigger generation with path to the Android project
new DaoGenerator().generateAll(schema, "../project/src/main/java");
}
}