1

I am new to Greendao.I am writing a generator for generating entities in greendao.So,I have two entities Hospital and patient. They have a one to many relationship between them. So,a hospital can have many patients but one patient can have only one hospital. Now Property hospitalId = patient.addLongProperty("hospitalId").getProperty(); this adds a hospitalid column to patient table. and

ToMany hospitalToPatients = hospital.addToMany(patient, hospitalId);

This line creates a one to many relationship between hospital and patient.So what is the use of the lines patient.addToOne(hospital, hospitalId); and hospitalToPatients.setName("patients"); How to implement one to one,one to many,many to one and many to many relationships in greendao ? PS: I copied this code from 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");
        }
    }
Prateek Ratnaker
  • 817
  • 11
  • 35

1 Answers1

-1

So what is the use of the lines patient.addToOne(hospital, hospitalId)

This line is creating a oneToOne relation between hospital and patient .

hospitalToPatients.setName("patients")   

This is just setting the name of foreign key .

As you can see, you have already implemented implement one to one,one to many relationship in your example . patient.addToOne is an example of OneToOne relationships . hospital.addToMany is an example of OneToMany relationships . And greenDao doesn't support ManyToMany relationship directly for more details you can read this .

Community
  • 1
  • 1
Mithun Sarker Shuvro
  • 3,902
  • 6
  • 32
  • 64
  • if i want to create a one to many relationship between patient and hospital such that one patient is related to one hospital and one hospital can have many patients so the line Property hospitalId = patient.addLongProperty("hospitalId").getProperty(); would add a foreign key in patient table and create an attribute hospitalID in patient entity...now ToMany hospitalToPatients = hospital.addToMany(patient, hospitalId); hospitalToPatients.setName("patients"); this would create one to many relationship between hospital and patient and add a List to hospital entity ...right?? – Prateek Ratnaker Oct 23 '16 at 11:56
  • now since we have created a one to many relationship between hospital and patient ....so what is the use of the line patient.addToOne(hospital, hospitalId); in the above code...since we have already implemented the relationship....has it got something to do with bidirectionality?? – Prateek Ratnaker Oct 23 '16 at 11:59
  • @PrateekRatnaker Property hospitalId = patient.addLongProperty("hospitalId").getProperty(); it wont create the relationship , you are just building a property . You are creating OneToOne relation by patient.addToOne(hospital, hospitalId) and by this line you will have hospitalId attribute in patient table . I don't think it has nothing to do with bidirectionality . – Mithun Sarker Shuvro Oct 23 '16 at 12:43
  • ToMany hospitalToPatients = hospital.addToMany(patient, hospitalId); hospitalToPatients.setName("patients"); ...this line would create a one to many relationship between hospital and patient ...so now why the line patient.addToOne(hospital, hospitalId) has been written ?since Tomany has created a one to many relationship already so why do we need patient.addToOne(hospital, hospitalId)? PS: we can implement only one relation beetween two entities ....so either one to one OR one to many but why have we used both ToOne and Tomany in above example? – Prateek Ratnaker Oct 23 '16 at 14:33
  • Lets say , you want to set the hospital for a patient , every time you insert a new entry for patient , you have the ability to set the hospital for that patient . because you have OneToOne relation between patient and hospital . similarly you can get the hospital of the patient vice versa . Lets say , you want to get all the patients of a hospital , here is why addToMany is used . If you check the generated model class , you can understand better . – Mithun Sarker Shuvro Oct 23 '16 at 15:28
  • Property hospitalId = patient.addLongProperty("hospitalId").getProperty();...this line adds hospitalId foreign key to patient table and does it map to primary key defined as hospital.addIdProperty(); ??? 2.ToOne creates patient model such that you can fetch corresponding hospital object from patient object and TOMany creates Hospital model such that you can fetch List from any Hospital object...right?? – Prateek Ratnaker Oct 24 '16 at 02:39
  • 1
    No hospitalId = patient.addLongProperty("hospitalId").getProperty(), this line only adding a property to entity , and later when you crate relation like addToOne , you used that property as foreign key . For 2 . yes – Mithun Sarker Shuvro Oct 24 '16 at 05:03
  • can we insert primary key starting with a particular number....i need the primary keys to be negative...how can this be done in greendao? – Prateek Ratnaker Oct 24 '16 at 12:18
  • 1
    I am not sure about that , I think whether the primary keys can be negative or not , it doesn't depend on greendao , if depends on sqlite – Mithun Sarker Shuvro Oct 24 '16 at 12:23
  • greendao automatically generates primary keys by using addId() function and autoincrement/autodecrement starts increasing/decreasing the sequence ....can't i start the primary key insertion from a particular number like -1....obviously the database will be able to handle this since the datatype of the id field would be number .but how to control the starting point or number from which primary keys get created and are inserted sequentially.Eg. the first object that is inserted in hospital table must have a id -1,the second object must have a primary key -2 and so on and so forth.how to do this? – Prateek Ratnaker Oct 24 '16 at 14:17
  • 1
    though greendao automatically generates primary key but you can also do it manually . like entity.addIntProperty("id").autoincrement().primaryKey(); or entity.addIntProperty("id").primaryKey(); . – Mithun Sarker Shuvro Oct 25 '16 at 07:33
  • what's the use of autoincrement in entity.addIntProperty("id").autoincrement().primaryKey(); ...for example while creating the entity ,i don't give any value for primary key field...so what value will grendao assign for it or it will give some error.......2. Can't i start the autogeneration of primary keys as supported by greendao from a specific number? – Prateek Ratnaker Oct 25 '16 at 12:43