Spring引导Dbref
我是 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
处方数据库
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
Prescription Db
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须在保存后设置药品 ID:
并在您的实体中:
You must set Medicines Ids after saving:
and in your entity: