[英]How to do the CURD operations on Spring Boot Restful API using Sping Data Jpa OneToMany nested model?
[英]How do i make spring boot jpa operations wait for each other
我的 class 中有兩個 JPA 操作。 病人保存和電話保存操作。 我想在病人得救后保存電話。 操作似乎當時正在發生。 患者已成功保存,但 phoneService.addPatientPhone(phone, patientId); 即使 patientResolver.getPhone() 包含數字,也永遠不會被執行。
**Service class**
public Patient createPatient(PatientResolverDTO patientResolver, String patientId) throws Exception {
Patient checkExist = patientRepository.findByPatientId(patientId);
Patient patient = new Patient();
if (checkExist != null) {
log.debug("Patient already exit: {}", request);
return request;
} else {
patient.setPatientId(patientId);
patient.setFirstname(patientResolver.getFirstname());
patient.setLastname(patientResolver.getLastname());
patient.setDob(patientResolver.getBirthdate());
Patient savedPatient = patientRepository.save(patient);
/**
* If the payload containts phone(s). Add the phone number(s) to the
* patient Phone
*/
if (patientResolver.getPhone() != null && savedPatient != null) {
for (PatientResolverPhone phone : patientResolver.getPhone()) {
phoneService.addPatientPhone(phone, patientId);
}
}
return savedPatient;
}
}
**Repository class**
/**
* Spring Data JPA repository for the {@link Patient} entity.
*/
@Repository
public interface PatientRepository extends JpaRepository<Patient, Long> {
Patient findOneByPatientId(String patientId);
Patient findByPatientId(String patientId);
}
您在存儲庫上使用Long
而不是String
:
public interface PatientRepository extends JpaRepository<Patient, String>
如果添加了數字,您是否檢查過您的數據庫? 如果是,則問題是您返回已保存的患者,而此行中沒有更新信息:
Patient savedPatient = patientRepository.save(patient);
如果您想用更新的信息返回患者,您需要再次檢索患者或將手機設置為您已經創建的 object
PatientResolverPhone prf = phoneService.addPatientPhone(phone, patientId); savedPatient.setPatientResolverPhone(prf); return savedPatient;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.