Spring boot Dbref

2.1k Views Asked by At

I am new to spring boot and having trouble resolving dbref.
On a spring boot project I am using MongoDB with dbref. I want to create two collections prescription and medicines. Prescription has a list of Medicines.

@Document
public class Prescription{
    @Id
    private String id;
    private String doctorId;
    private String patientName;

    @NotBlank
    private String patientEmailId;

    @NotNull
    private int duration;

    @DBRef(db="medicines", lazy = true)
    private List<Medicines> medicines = new ArrayList<Medicines>();

    //getters and setters 
}

@Document
public class Medicines{
    @Id
    private String id;

    @NotBlank
    private String medicineName;

    @NotNull
    private int dosage;

    @NotNull
    private int [] timings;

    // getters and setters
}

I am able to find data correctly when connecting directly to database as I am using MongoDb Cloud. But when I fetch the data list of medicines it is null. This is the code I am using to fetch all data.

    public List<Prescription> getAllPrescriptions(){
        List<Prescription> p = prescriptionRepository.findAll();
        for(Prescription pr : p){
            System.out.println(pr.getMedicines());
        }
        return p;
    }

This is result of the get request (these are two same prescription added twice)

[
    {
        "id": "62190186aedad32aa7bec864",
        "doctorId": "621900fbaedad32aa7bec85b",
        "patientName": "devas",
        "patientEmailId": "[email protected]",
        "duration": 7,
        "medicines": []
    },
    {
        "id": "62190187aedad32aa7bec867",
        "doctorId": "621900fbaedad32aa7bec85b",
        "patientName": "devas",
        "patientEmailId": "[email protected]",
        "duration": 7,
        "medicines": []
    }
]

This is the result i am getting in terminal after using getAllPrescriptions() method

0$LazyLoadingProxy
0$LazyLoadingProxy

This is how I insert

public ResponseEntity<Prescription> insertPrescription(Prescription newPrescription, String AccessToken){
    List<Medicines> m= new ArrayList<Medicines>();
    for(Medicines medicines: newPrescription.getMedicines()){
        medicinesRepository.insert(medicines);
        m.add(medicines);
    }
    newPrescription.setMedicines(m);
    prescriptionRepository.insert(newPrescription);
    return ResponseEntity.accepted().body(newPrescription);
}

Why is this happening and how can I solve this?

Edit : This is how my database looks like-

Medicine Db

Medicine db

Prescription Db

Prescription db

1

There are 1 best solutions below

6
Mohammad Javad On

You must set Medicines Ids after saving:

public ResponseEntity<Prescription> insertPrescription(Prescription newPrescription, String AccessToken){
List<Medicines> m= new ArrayList<Medicines>();
for(Medicines medicines: newPrescription.getMedicines()){
    medicines = medicinesRepository.insert(medicines);
    m.add(medicines);

newPrescription.setMedicines(m);
prescriptionRepository.insert(newPrescription);
return ResponseEntity.accepted().body(newPrescription);
}

and in your entity:

@DBRef
private List<Medicines> medicines;