Spring引导Dbref

发布于 2025-01-10 05:29:40 字数 2877 浏览 0 评论 0原文

我是 Spring Boot 新手,在解析 dbref 时遇到问题。
在 Spring Boot 项目中,我使用 MongoDB 和 dbref。我想创建两个处方和药品集合。处方上有一份药物清单。

@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
}

当我使用 MongoDb Cloud 直接连接到数据库时,我能够正确找到数据。 但是当我获取药品数据列表时,它是空的。 这是我用来获取所有数据的代码。

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

这是获取请求的结果(这是两个相同的处方添加了两次)

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

这是我使用 getAllPrescriptions() 方法后在终端中得到的结果

0$LazyLoadingProxy
0$LazyLoadingProxy

这是我插入的方式

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

为什么会发生这种情况以及如何解决这个问题?

编辑 : 这就是我的数据库的样子 -

Medicine Db

Medicine db

处方数据库

处方数据库

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

动听の歌 2025-01-17 05:29:40

您必须在保存后设置药品 ID:

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

并在您的实体中:

@DBRef
private List<Medicines> medicines;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文