[英]How do i make spring boot jpa operations wait for each other
I have two JPA operations in my class.我的 class 中有两个 JPA 操作。 patient save and phone save operations.病人保存和电话保存操作。 I want phone to save after patient has been saved.我想在病人得救后保存电话。 The operations seem to be happening at the time.操作似乎当时正在发生。 The patient is saved successfully but the phoneService.addPatientPhone(phone, patientId);患者已成功保存,但 phoneService.addPatientPhone(phone, patientId); is never excecuted even if patientResolver.getPhone() containts numbers.即使 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);
}
You are using Long
instead of String
on your Repository:您在存储库上使用Long
而不是String
:
public interface PatientRepository extends JpaRepository<Patient, String>
Have you checked on your database if the number is added?如果添加了数字,您是否检查过您的数据库? If yes the problem is that you are returning the saved patient without the updated information in this line:如果是,则问题是您返回已保存的患者,而此行中没有更新信息:
Patient savedPatient = patientRepository.save(patient);
If you wanto to return the patient with the updated info you need to retrieve the patient again or set the phone to te object that you already have created如果您想用更新的信息返回患者,您需要再次检索患者或将手机设置为您已经创建的 object
PatientResolverPhone prf = phoneService.addPatientPhone(phone, patientId); savedPatient.setPatientResolverPhone(prf); return savedPatient;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.