我使用greendao生成器生成了两个实体。这两个实体是医院和病人。医院与病人之间存在着一对多的关系。所以一个医院可以有多个病人,一个病人只能有一个医院。所以医院会有一份病人名单。
Greendao生成器的代码是->

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”的病人对象。现在这两个病人有“一号医院”作为他们的相关医院。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的列表的对应值。生成的JSON字符串为:
{
            "Hospital_Name":"Hospital 1", "id":1
}

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

最佳答案

看看Relations文档:一对多的关系在访问它们时被懒洋洋地解决了
多对多关系,在第一次访问时解决(重置后)
所以只需要调用hospital1.getPatients();然后再将对象转换为JSON。所以添加这一行之后,您的代码将如下所示。

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

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

我希望它能解决你的问题

08-18 18:26