我有一个应用程序,其用例是患者或医生有一个或多个预约。
我有这样的实体:实体
Patient
:
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Patient {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long idPatient;
@Column(length = 80)
private String name;
private String mail;
@Temporal(TemporalType.DATE)
private Date dateNaissance;
private boolean malade;
@OneToMany(mappedBy = "patient", fetch = FetchType.LAZY)
private Collection<RendezVous> rendezVousPatient;
}
还有实体
Doctor
:
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Medecin {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long idMedecin;
@Column(length = 80)
private String name;
private String email;
private String speciality;
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
@OneToMany(mappedBy = "medecin", fetch = FetchType.LAZY)
private Collection<RendezVous> rendezVousMedecin;
}
这是
Appointment
实体:
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class RendezVous {
@Id
private String idRDV;
@Temporal(TemporalType.DATE)
private Date dateRDV;
@Enumerated(EnumType.STRING)
private StatusRDV status;
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
@ManyToOne
private Patient patient;
@ManyToOne
private Medecin medecin;
@OneToOne(mappedBy = "rendezVous")
}
每个实体都有其 DTO,以下是 DTO 中的实体:
Patient
的 DTO:
@Data
public class PatientDTO {
private Long idPatient;
@NotNull(message = "Name does not null")
private String name;
@Email(message = "Email is not Valid")
private String mail;
private Date dateNaissance;
private boolean malade;
private Collection<RendezVous> rendezVousPatient;
}
Doctor
的DTO:
@Getter
@Setter
public class MedecinDTO {
private Long idMedecin;
@NotBlank(message = "Name does not Null")
private String name;
@Email(message = "Mail not Valid")
private String email;
@NotNull(message = "the doctor must have a speciality. ")
private String speciality;
private Collection<RendezVous> rendezVousMedecin;
}
以及
Appointment
的最终DTO:
@Data
public class RendezVousDTO {
private String idRDV;
private Date dateRDV;
private StatusRDV status;
private Patient patient;
private Medecin medecin;
}
在实现的服务中是Update和Delete
的代码@Service
@Transactional
public class IhospitalImpl implements Ihospital {
Logger logger = LoggerFactory.getLogger(IhospitalImpl.class);
@Autowired
private PatientMapperImpl patientMapper;
@Autowired
private MedecinMapperImpl medecinMapper;
@Autowired
private RendezVousMapper rendezVousMapper;
@Override
public MedecinDTO updateMedecin(MedecinDTO medecinDTO, Long id) throws
MedecinNotFoundException, RendezVousNotFound {
Medecin medecin = medecinMapper.fromMedecinDTO(medecinDTO);
Medecin currentMedecin = medecinMapper.fromMedecinDTO(findMedecinById(id));
if (!medecin.getEmail().isEmpty()) {
currentMedecin.setEmail(medecin.getEmail());
}
if (!medecin.getName().isEmpty()) {
currentMedecin.setName(medecin.getName());
}
if (medecin.getRendezVousMedecin() != null) {
currentMedecin.setRendezVousMedecin(medecin.getRendezVousMedecin());
}
if (!medecin.getSpeciality().isEmpty()) {
currentMedecin.setSpeciality(medecin.getSpeciality());
}
MedecinDTO savedMedecinDTO =
medecinMapper.fromMedecin(medecinRepository.save(currentMedecin));
return savedMedecinDTO;
}
@Override
public PatientDTO upDatePatient(PatientDTO patientDTO, Long id) throws
PatientNotFoundException {
Patient patient = patientMapper.fromPatientDTO(patientDTO);
Patient currentPatient = patientMapper.fromPatientDTO(findPatienById(id));
if (patient.getDateNaissance() != null) {
currentPatient.setDateNaissance(patient.getDateNaissance());
}
if (patient.getMail() != null) {
currentPatient.setMail(patient.getMail());
}
if (patient.getName() != null) {
currentPatient.setName(patient.getName());
}
if (patient.getRendezVousPatient() != null) {
currentPatient.setRendezVousPatient(patient.getRendezVousPatient());
}
PatientDTO savedPatient =
patientMapper.fromPatient(patientRepository.save(currentPatient));
return savedPatient;
}
@Override
public RendezVousDTO updateRendezVous(RendezVousDTO rendezVousDTO, String id) throws
RendezVousNotFound {
RendezVous rendezVous = rendezVousMapper.fromRendeVousDTO(rendezVousDTO);
RendezVous currentRendezVous = rendezVousMapper.fromRendeVousDTO(findRDVById(id));
if (rendezVous.getConsultation() != null) {
currentRendezVous.setConsultation(rendezVous.getConsultation());
}
if (rendezVous.getDateRDV() != null) {
currentRendezVous.setDateRDV(rendezVous.getDateRDV());
}
if (rendezVous.getMedecin() != null) {
currentRendezVous.setMedecin(rendezVous.getMedecin());
}
if (rendezVous.getPatient() != null) {
currentRendezVous.setPatient(rendezVous.getPatient());
}
if (rendezVous.getStatus() != null) {
currentRendezVous.setStatus(rendezVous.getStatus());
}
RendezVousDTO savedRDV =
rendezVousMapper.fromRendezVous(rendezVousRepository.save(currentRendezVous));
return savedRDV;
}
}
@Override
public Map<String, Boolean> deletePatient(Long id) throws PatientNotFoundException {
Patient patient = patientRepository.findById(id).orElseThrow(() -> new
PatientNotFoundException("Patient Not Found with id : " + id));
patientRepository.delete(patient);
Map<String, Boolean> mapDelete = new HashMap<>();
mapDelete.put("Delete Patient", Boolean.TRUE);
return mapDelete;
}
@Override
public Map<String, Boolean> deleteMedecin(Long id) throws MedecinNotFoundException {
MedecinDTO medecinDTO = getMedecin(id);
medecinRepository.delete(medecinMapper.fromMedecinDTO(medecinDTO));
Map<String, Boolean> mapDelete = new HashMap<>();
mapDelete.put("Delete Medecin", Boolean.TRUE);
return mapDelete;
}
@Override
public Map<String, Boolean> deleteRDV(String id) throws RendezVousNotFound {
RendezVousDTO rendezVousDTO = findRDVById(id);
rendezVousRepository.delete(rendezVousMapper.fromRendeVousDTO(rendezVousDTO));
Map<String, Boolean> mapDelete = new HashMap<>();
mapDelete.put("Delete Rendez vous", Boolean.TRUE);
return mapDelete;
}
}
如果我要修改其中一个 DTO,就会出现问题:患者或医生的属性有效,但其他修改无法修改。例如,如果我想修改属性名称,则可以通过邮件发送,但如果我想修改约会,则不能。约会也是如此。
我尝试使用 POSTMAN,但调试时仍然出现异常
方法抛出“org.hibernate.LazyInitializationException”异常。 无法评估 com.example.Entities.Patient.toString()
无需在 intellij 控制台进行调试
java.lang.NullPointerException:空
以及删除如果我想删除有或没有预约的患者。删除成功。但是问题是数据库中删除了患者,但是数据库中已经存在与患者或医生相关的预约,并且使用 getRDV 命令时显示异常消息,预约不存在。
我希望我已经解释得很好,并感谢所有帮助我的人。
只要进行两次更改就可以正常工作。 让我们关注患者。 如果您在 Patient 实体中添加 CascadeType,它将随之更新 RendezVous 实体。
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Patient {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long idPatient;
@Temporal(TemporalType.DATE)
private Date dateNaissance;
@Column(length = 80)
private String name;
private String mail;
private boolean malade;
@OneToMany(mappedBy = "patient", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Collection<RendezVous> rendezVousPatient;
}
映射者还需要进行一项更改。如果您显式设置 rendezVousPatient 的 Patient 并将该 rendezVousPatient 提供给患者,它将随之更新其他值,就像下面的代码一样:
@Component
public class PatientMapper {
public Patient fromPatientDTO(PatientDTO patientDTO) {
Patient patient=new Patient();
BeanUtils.copyProperties(patientDTO,patient);
for(RendezVous rendezVous:patientDTO.getRendezVousPatient())
rendezVous.setPatient(patient);
patient.setRendezVousPatient(patientDTO.getRendezVousPatient());
return patient;
}
public PatientDTO fromPatient(Patient patient) {
PatientDTO patientDTO=new PatientDTO();
BeanUtils.copyProperties(patient,patientDTO);
return patientDTO;
}
}
按照你说的情况,你的数据已经保存在数据库中,但是无法正确返回到你的控制页面,并且你的程序在使用postman的时候会有响应,但是对应的缺少无法映射到对应类的参数上,您的控制器上映射的路径和参数是否错误?我希望你能展示你的控件类,当数据库可以保存信息时,你的逻辑应该没问题,但是返回的数据映射应该有错误,你可以尝试用你的字段名传递参数同意,希望这有帮助你。
对于我的控制器来说是一样的
@RestController
@RequestMapping("/api/rdv")
public class RendezVousController {
@Autowired
private Ihospital ihospital;
@GetMapping("/rendezVousById/{id}")
public ResponseEntity<RendezVousDTO> getRDV(@PathVariable("id") String id) throws RendezVousNotFound {
return new ResponseEntity<>(ihospital.findRDVById(id), HttpStatus.OK);
}
@GetMapping("/rendezVous")
public ResponseEntity<List<RendezVousDTO>> getAllRDV() throws RendezVousNotFound {
return new ResponseEntity(ihospital.findAllRDV(), HttpStatus.OK);
}
@PostMapping("/rendezVousPost")
public ResponseEntity<RendezVousDTO> saveRDV(@RequestBody RendezVousDTO rendezVousDTO) {
return new ResponseEntity<>(ihospital.saveRDV(rendezVousDTO), HttpStatus.CREATED);
}
@PutMapping("rendezVousPut/{id}")
public ResponseEntity<RendezVousDTO> updateRendezVous(@RequestBody RendezVousDTO rendezVousDTO, @PathVariable("id") String id) throws RendezVousNotFound {
return ResponseEntity.ok(ihospital.updateRendezVous(rendezVousDTO, id));
}
@DeleteMapping("/rendezVousDelete/{id}")
public ResponseEntity<Map<String, Boolean>> deleteRDV(@PathVariable("id") String id) throws RendezVousNotFound {
Map<String, Boolean> response = ihospital.deleteRDV(id);
return ResponseEntity.ok(response);
}
}
控制器的医生和患者也是如此
我只能修改 MedecinDTO 的直接属性,例如姓名、电子邮件和专业性。以及患者 DTO 的姓名、电子邮件和出生日期。还有 RendezVousDTO 的 dateRDV 和状态,但我无法通过 RendezVousDTO 修改患者或医生 我不知道规则是否意味着这一点,或者我不知道如何使用邮递员来做到这一点。问题是我能还是不能 谢谢 我可以在直接控制器中删除或修改任何 DTO,但无法在另一个控制器中删除或修改