使用GSON序列化时的Greendao实体不包含Generated JSON string

时间:2016-12-13 02:07:20

标签: android gson greendao

我有两个使用Greendao生成器生成的实体。这两个实体是医院和患者。医院和患者之间存在一对多的关系。因此,一家医院可以有多名患者,一名患者只能有一家医院。因此,医院将包含患者名单。

greendao Generator的代码是>

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");



    }

}

因此,在我的主要活动中,我创建了一个名为“Hospital 1”的医院对象和两个名为“Patient 1”和“Patient 2”的患者对象。现在这两名患者将“医院1”作为他们的相关医院。 patient1.getHospital()将返回Hospital1对象,patient2.getHospital()也将返回Hospital1对象。 Hospital1.getPatient()将返回包含patient1和patient2对象的患者列表。

我的活动代码是 - >

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();
*/


    }
}

但是当我从数据库中检索Hospital1对象并使用GSON检索它时。生成的JSON字符串不包含任何patientList键和包含Patient1和Patient2的List的相应值。生成的JSON字符串为:

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

因此,生成的JSON字符串不包含医院实体的所有属性。如何解决这个问题?

2 个答案:

答案 0 :(得分:3)

查看Relations文档:在访问它们时懒得解决​​to-one和to-many关系

多对多关系,在首次访问时(以及重置后)解决

所以只需要在将对象转换为Json之前调用 hospital1.getPatients(); 。添加此行后,您的代码将如下所示。

Usage: Dictionary<string, string> dict = stringToDictionary("userid=abc&password=xyz&retain=false");

public static Dictionary<string, string> stringToDictionary(string line, char stringSplit = '&', char keyValueSplit = '=')
{
    return line.Split(new[] { stringSplit }, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Split(new[] { keyValueSplit })).ToDictionary(x => x[0], y => y[1]); ;
}

我希望它能解决你的问题

答案 1 :(得分:3)

首先,您的Hospital类需要有一个列表参数才能保存患者对象。它可以是ArrayList<Patinet>,然后您需要从数据库中获取患者列表

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));