0

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": "devashishdeshpande@gmail.com",
        "duration": 7,
        "medicines": []
    },
    {
        "id": "62190187aedad32aa7bec867",
        "doctorId": "621900fbaedad32aa7bec85b",
        "patientName": "devas",
        "patientEmailId": "devashishdeshpande@gmail.com",
        "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

Devas
  • 28
  • 7

1 Answers1

0

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;
  • doesn't `id` get auto-generated upon saving or inserting? i tried debugging and it works and saved in proper tables but it is not getting retrieved and when i intialize arraylist `Arraylist()` like you said intellij shows `Redundant initializer`. – Devas Feb 26 '22 at 08:25
  • you don't need to initialize it. just in DbRef, set lazy to false or delete lazy properties, because the default value of lazy is false – Mohammad Javad Feb 26 '22 at 08:58
  • `[]` `[]` this is the output it shows after using lazy=false on the entity and using `getAllPrescription` – Devas Feb 26 '22 at 09:19
  • 1
    please check this [link](https://github.com/mjimani/stackOverFlow-spring-mongo) – Mohammad Javad Feb 26 '22 at 10:49
  • Thank you very much for your effort but this is exactly how I have written just not on command liner that is in main function. Is this working for you? – Devas Feb 26 '22 at 12:20
  • Yes, this is working for me. – Mohammad Javad Feb 26 '22 at 13:22