Spring CrudRepository-如何通过外键ID插入记录?

问题描述 投票:0回答:1

[使用后请求外键相关参考记录插入记录时未链接。

实体类:

@Entity
@Table(name = "patient")
public class Patient implements java.io.Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "patient_id")
    private int patientId;  

    @Column(name = "patient_name", length = 200) 
    private String patientName; 

    @Column(name = "problem", length = 200) 
    private String problem;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "doctor_id", nullable = false, insertable = false, updatable = false)
    private Doctor doctor;  

}

@Entity
@Table(name = "doctor")
public class Doctor implements java.io.Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "doctor_id")
    private int doctorId;   

    @Column(name = "doctor_name", length = 200) 
    private String doctorName;  

    @Column(name = "department", length = 200) 
    private String department;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "doctor")
    private Set<Patient> patients = new HashSet<Patient>(0);

}

数据库-Doctor Table:doctor_id doctor_name科室12345678 Dfirstname Dlastname ENT

POST请求-JSON正文{“ patientName”:“ Pfirstname Plastname”,“问题:“可见性问题-光线不足”,“ doctor”:{“ doctorId”:“ 12345678”}}

当我发送此请求时,患者表doctor_id列未使用docortId填充。

java spring-boot spring-data-jpa hibernate-mapping
1个回答
0
投票

乍看之下(因为未提供服务层)您必须从@JoinColumn中删除insertable = false和updatable = false

@JoinColumn(name = "doctor_id", nullable = false, insertable = false, updatable = false)

将其更改为:

@JoinColumn(name = "doctor_id", nullable = false)

因为此指令不允许jpa插入/更新DOCTOR_ID

此外,我更喜欢使用原始类型的包装器,因为@Id将int更改为Integer,如此处Using wrapper Integer class or int primitive in hibernate mapping所示

而且似乎您已经坚持了doctor(因为它已经分配了ID),您应该首先选择db的医生,并在两端为其添加患者:

public void assignToDoctor(Doctor doctor) {
        doctor.patients.add(this);
        this.doctor = doctor;
}

这里是完整的示例:

    public static void main(String[] args) {
        SpringApplication.run(DemostackApplication.class, args);
    }


    @Component
    public static class AppRunner implements ApplicationRunner {

        @Autowired
        MainService mainService;

        @Override
        public void run(ApplicationArguments args) throws Exception {
            Doctor doctor = new Doctor();
            doctor.department = "a";
            doctor.doctorName = "Covid19 Champion Doctor";
            doctor = mainService.saveDoctor(doctor);

            Patient patient = new Patient();
            patient.patientName = "test";
            patient.problem = "test";
            patient.assignToDoctor(doctor);
            Patient newPatient = mainService.savePatient(patient);
        }
    }

    @Service
    public static class MainService {
        @Autowired
        DoctorRepo doctorRepo;
        @Autowired
        PatientRepo patientRepo;

        @Transactional
        public Doctor saveDoctor(Doctor doctor) {
            return doctorRepo.save(doctor);
        }

        @Transactional
        public Patient savePatient(Patient patient) {
            return patientRepo.save(patient);
        }
    }

    @Entity
    @Table(name = "patient")
    public static class Patient implements java.io.Serializable {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "patient_id")
        private Integer patientId;

        @Column(name = "patient_name", length = 200)
        private String patientName;

        @Column(name = "problem", length = 200)
        private String problem;

        @ManyToOne(fetch = FetchType.LAZY)
        @JoinColumn(name = "doctor_id", nullable = false)
        private Doctor doctor;

        public void assignToDoctor(Doctor doctor) {
            doctor.patients.add(this);
            this.doctor = doctor;
        }
    }

    @Entity
    @Table(name = "doctor")
    public static class Doctor implements java.io.Serializable {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "doctor_id")
        private Integer doctorId;
        @Column(name = "doctor_name", length = 200)
        private String doctorName;

        @Column(name = "department", length = 200)
        private String department;

        @OneToMany(fetch = FetchType.LAZY, mappedBy = "doctor")
        private Set<Patient> patients = new HashSet<Patient>(0);
    }

我没有使用getter / setter,但是您应该:)

© www.soinside.com 2019 - 2024. All rights reserved.