简体   繁体   English

我如何让 spring 启动 jpa 操作相互等待

[英]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);

}

  1. You are using Long instead of String on your Repository:您在存储库上使用Long而不是String

    public interface PatientRepository extends JpaRepository<Patient, String>

  2. 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);

  3. 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.

相关问题 如何使用Sping Data Jpa OneToMany嵌套模型在Spring Boot Restful API上执行CURD操作? - How to do the CURD operations on Spring Boot Restful API using Sping Data Jpa OneToMany nested model? 如何使Spring Boot Security和JBoss 7.1相互配合? - How do I get Spring Boot Security and JBoss 7.1 to play nicely with each other? Spring Boot JPA:如何连接多个数据库? - Spring Boot JPA: How do I connect multiple databases? 在Spring Boot Jpa中相互连接三个实体时出现问题 - Problems wiring up three Entities in Spring Boot Jpa with each other 我想在彼此之后播放声音,但让它们等到第一个完成。 我该怎么做? - I want to play sounds after each other but make them wait until the first one is done. How do I do it? 如何使我的其他操作正常工作? - How do I make my other operations work properly? JPA + Spring Boot:CRUD操作上的TransactionRequiredException - JPA + Spring Boot: TransactionRequiredException on CRUD operations 我该如何使用属性而不直接在实体休眠Spring JPA上进行findBy操作 - How can I do findBy operations with attributes not directly on the entity hibernate spring JPA 如何使用spring-boot,spring-data-jpa和hibernate获得“无级联”的行为? - How do I get “no cascading”-behaviour using spring-boot, spring-data-jpa and hibernate? 我如何在没有Spring Security的情况下使用Spring Boot + JPA进行简单登录 - How Can i do a simple login with spring boot + JPA without spring security
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM