Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何制作Restful API来处理Spring引导中的多对多关系?_Java_Spring Boot - Fatal编程技术网

Java 如何制作Restful API来处理Spring引导中的多对多关系?

Java 如何制作Restful API来处理Spring引导中的多对多关系?,java,spring-boot,Java,Spring Boot,在我的Spring boot项目中,我有两个表名-医生和患者。在这些表中,我有以下属性- 现在,我想在这两个表之间为预约创建一个多对多关系,因为一个医生可以有多个病人,一个病人可以预约多个医生。因此,为了处理这个问题,我创建了另一个名为appointment的表,它将把doctorId和patientId作为外键 我需要使用JSON请求体创建约会,如下所示- 为此,我创建了一个模型类,如下所示- Appointment.java @Entity @Table(name = "appoint

在我的Spring boot项目中,我有两个表名-医生患者。在这些表中,我有以下属性-

现在,我想在这两个表之间为预约创建一个多对多关系,因为一个医生可以有多个病人,一个病人可以预约多个医生。因此,为了处理这个问题,我创建了另一个名为appointment的表,它将把doctorId和patientId作为外键

我需要使用JSON请求体创建约会,如下所示-

为此,我创建了一个模型类,如下所示-

Appointment.java

@Entity
@Table(name = "appointments")
public class Appointment {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotNull
    private Long appointedDoctorId;

    @NotNull
    private Long appointedPatientId;

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


    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "patientId", nullable = false)
    private Patient patient;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }


    public Long getAppointedDoctorId() {
        return appointedDoctorId;
    }

    public void setAppointedDoctorId(Long appointedDoctorId) {
        this.appointedDoctorId = appointedDoctorId;
    }

    public Long getAppointedPatientId() {
        return appointedPatientId;
    }

    public void setAppointedPatientId(Long appointedPatientId) {
        this.appointedPatientId = appointedPatientId;
    }

    public Doctor getDoctor() {
        return doctor;
    }

    public void setDoctor(Doctor doctor) {
        this.doctor = doctor;
    }

    public Patient getPatient() {
        return patient;
    }

    public void setPatient(Patient patient) {
        this.patient = patient;
    }
}
@Repository
public interface AppointmentRepository extends JpaRepository<Appointment, Long> {

    Page<Appointment> findByDoctorId(Long id, Pageable pageable);

}
public interface AppointmentService {

    public Page<Appointment> getAllAppointmentByDoctorId(@PathVariable(value = "doctorId") Long id, Pageable pageable);

    public Appointment createAppointment(@Valid @RequestBody Appointment createAppointmentRequest);

}
@Service
public class AppointmentServiceImpl implements AppointmentService {

    @Autowired
    AppointmentRepository appointmentRepository;


    @Override
    public Page<Appointment> getAllAppointmentByDoctorId(Long id, Pageable pageable) {
        return appointmentRepository.findByDoctorId(id, pageable);
    }

    @Override
    public Appointment createAppointment(@Valid Appointment createAppointmentRequest) {
        return appointmentRepository.save(createAppointmentRequest);
    }
}
@RestController
@RequestMapping("/api")
public class AppointmentController {

    @Autowired
    AppointmentService appointmentService;

    @GetMapping("/doctors/{doctorId}/appointments")
    public Page<Appointment> getAllAppointmentsByDoctorId(@PathVariable(value = "id") Long id, Pageable pageable){
        return appointmentService.getAllAppointmentByDoctorId(id, pageable);
    }


    @PostMapping("/insert/new/appointments")
    public Appointment createAppointment(@Valid Appointment createAppointmentRequest) {
        return appointmentService.createAppointment(createAppointmentRequest);
    }

}
这是存储库类-

AppointRepository.java

@Entity
@Table(name = "appointments")
public class Appointment {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotNull
    private Long appointedDoctorId;

    @NotNull
    private Long appointedPatientId;

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


    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "patientId", nullable = false)
    private Patient patient;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }


    public Long getAppointedDoctorId() {
        return appointedDoctorId;
    }

    public void setAppointedDoctorId(Long appointedDoctorId) {
        this.appointedDoctorId = appointedDoctorId;
    }

    public Long getAppointedPatientId() {
        return appointedPatientId;
    }

    public void setAppointedPatientId(Long appointedPatientId) {
        this.appointedPatientId = appointedPatientId;
    }

    public Doctor getDoctor() {
        return doctor;
    }

    public void setDoctor(Doctor doctor) {
        this.doctor = doctor;
    }

    public Patient getPatient() {
        return patient;
    }

    public void setPatient(Patient patient) {
        this.patient = patient;
    }
}
@Repository
public interface AppointmentRepository extends JpaRepository<Appointment, Long> {

    Page<Appointment> findByDoctorId(Long id, Pageable pageable);

}
public interface AppointmentService {

    public Page<Appointment> getAllAppointmentByDoctorId(@PathVariable(value = "doctorId") Long id, Pageable pageable);

    public Appointment createAppointment(@Valid @RequestBody Appointment createAppointmentRequest);

}
@Service
public class AppointmentServiceImpl implements AppointmentService {

    @Autowired
    AppointmentRepository appointmentRepository;


    @Override
    public Page<Appointment> getAllAppointmentByDoctorId(Long id, Pageable pageable) {
        return appointmentRepository.findByDoctorId(id, pageable);
    }

    @Override
    public Appointment createAppointment(@Valid Appointment createAppointmentRequest) {
        return appointmentRepository.save(createAppointmentRequest);
    }
}
@RestController
@RequestMapping("/api")
public class AppointmentController {

    @Autowired
    AppointmentService appointmentService;

    @GetMapping("/doctors/{doctorId}/appointments")
    public Page<Appointment> getAllAppointmentsByDoctorId(@PathVariable(value = "id") Long id, Pageable pageable){
        return appointmentService.getAllAppointmentByDoctorId(id, pageable);
    }


    @PostMapping("/insert/new/appointments")
    public Appointment createAppointment(@Valid Appointment createAppointmentRequest) {
        return appointmentService.createAppointment(createAppointmentRequest);
    }

}
@存储库
公共接口任命存储库扩展了JpaRepository


所以,我需要知道我的代码中有什么问题,以及如何通过像我在JSON RequestBody请求中提到的那样提供doctorIdpatientId来创建预约POST request

看起来你已经回答了这个问题。你能检查一下你的列名是什么吗预约表中的
医生id
医生id
?问题已更新,请检查。