Reputation: 101
I have two JPA operations in my class. 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); is never excecuted even if patientResolver.getPhone() containts numbers.
**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);
}
Upvotes: 1
Views: 1100
Reputation: 36
You are using Long
instead of String
on your Repository:
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
PatientResolverPhone prf = phoneService.addPatientPhone(phone, patientId);
savedPatient.setPatientResolverPhone(prf);
return savedPatient;
Upvotes: 2