android - example - greendao save




Greendao entity when serialized using GSON doesn't contain related entity in Generated JSON string (2)

I have two entities Generated using Greendao generator. The two entities are Hospital and Patient. There exists a one to many relationship between Hospital and Patient. So one hospital can have multiple patients and one Patient can have only one hospital. So a Hospital will contain a list of patients.

Code for greendao Generator is->

package com.myapp.generator;


import org.greenrobot.greendao.generator.DaoGenerator;
import org.greenrobot.greendao.generator.Entity;
import org.greenrobot.greendao.generator.Property;
import org.greenrobot.greendao.generator.Schema;
import org.greenrobot.greendao.generator.ToMany;



public class MyClass {
    public static void main(String[] args) throws Exception {
        Schema schema = new Schema(1, "com.example.mohit.greendaotest.db");
        Entity hospital = schema.addEntity("Hospital");
        hospital.addIdProperty().autoincrement();
        hospital.addStringProperty("Hospital_Name");

        Entity patient=schema.addEntity("Patient");
        patient.addIdProperty().autoincrement();
        patient.addStringProperty("Patient_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");



        DaoGenerator dg=new DaoGenerator();
        dg.generateAll(schema,"./app/src/main/java");



    }

}

So in my main activity i have created one Hospital Object named "Hospital 1" and two patient Object named "Patient 1" and "Patient 2". Now the two patients have "Hospital 1" as their related hospital. patient1.getHospital() would return Hospital1 object and patient2.getHospital() would also return Hospital1 object. Hospital1.getPatient() would return a List of patients containing both patient1 and patient2 object.

My activity code is ->

import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;


import com.google.gson.Gson;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "lease-db", null);
        SQLiteDatabase db = helper.getWritableDatabase();
        DaoMaster daoMaster = new DaoMaster(db);
        DaoSession daoSession = daoMaster.newSession();

        Hospital hospital = new Hospital();
        hospital.setHospital_Name("Hospital 1");

        long hospital_id = daoSession.getHospitalDao().insert(hospital);

        Patient patient1 = new Patient();
        patient1.setPatient_Name("Patient 1");

        Patient patient2 = new Patient();
        patient2.setPatient_Name("Patient 2");


        patient1.setHospitalId(hospital_id);

        patient2.setHospitalId(hospital_id);

        /*patient1.setHospital(hospital);
        patient2.setHospital(hospital);*/

        daoSession.getPatientDao().insert(patient1);
        daoSession.getPatientDao().insert(patient2);


       /* daoSession.getHospitalDao().load(hospital_id).getPatients().add(patient1);
        daoSession.getHospitalDao().load(hospital_id).getPatients().add(patient2);

        daoSession.getHospitalDao().update(daoSession.getHospitalDao().load(hospital_id));*/


        daoSession.clear();

        DaoSession daoSession1 = daoMaster.newSession();

        Hospital hospital1 = daoSession1.getHospitalDao().loadByRowId(1);


        Gson gson = new Gson();
        Log.e("msg", gson.toJson(hospital1));





        /*Toast.makeText(this,String.valueOf(daoSession1.getPatientDao().loadAll().size()),Toast.LENGTH_SHORT).show();
*/


    }
}

However when i retrieve Hospital1 object from database and retrieve it using GSON. The Generated JSON string doesn't contain any patientList key and corresponding value of List containing Patient1 and Patient2.The generated JSON String is :

{
            "Hospital_Name":"Hospital 1", "id":1
}

So the Generated JSON String doesn't contain all the attributes of the Hospital Entity. How to resolve this ?


First of all, your Hospital class needs to have a list parameter in order to hold patient objects. It can be ArrayList<Patinet> then you need to get the list of patients from database,

Hospital hospital1 = daoSession1.getHospitalDao().loadByRowId(1);

//This block can change according to your design
ArrayList<Patient> patinets = daoSession.getPatientDao().getPatients(hospital1.getId());

hospital1.setPatinets(patinets);
Gson gson = new Gson();
Log.e("msg", gson.toJson(hospital1));

Have a look at the Relations docs: to-one and to-many relations are resolved lazily, on accessing them

To-many relationship, resolved on first access (and after reset)

So just need to call hospital1.getPatients(); before converting the object into Json.So After adding this line your code will look like below.

Hospital hospital1 = daoSession1.getHospitalDao().loadByRowId(1);
hospital1.getPatients();

Gson gson = new Gson();
Log.e("msg", gson.toJson(hospital1));

I hope it will solve your problem