title: Update and delete Entity contains another Entity One To many Many to one relationship java
body: <p>I have an application with the use case A Patient or Doctor has one or more appointments.
  I have the entities like this: Entiy <strong>Patient</strong></p>
  <pre><code>@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 = &quot;patient&quot;, fetch = FetchType.LAZY)
  private Collection&lt;RendezVous&gt; rendezVousPatient;
  }
  </code></pre>
  <p>And the entity of <strong>Doctor</strong></p>
  <pre><code>@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 = &quot;medecin&quot;, fetch = FetchType.LAZY)
  private Collection&lt;RendezVous&gt; rendezVousMedecin;
  }
  </code></pre>
  <p>Here the Entity of Appointment</p>
  <pre><code>@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 = &quot;rendezVous&quot;)
  </code></pre>
  <p>}
  Each entity has its DTO and here are the entities in DTO: DTO of <strong>Patient</strong></p>
  <pre><code>@Data
  public class PatientDTO {
  
  private Long idPatient;
  @NotNull(message = &quot;Name does not null&quot;)
  private String name;
  @Email(message = &quot;Email is not Valid&quot;)
  private String mail;
  private Date dateNaissance;
  private boolean malade;
  private Collection&lt;RendezVous&gt; rendezVousPatient;
  }
  </code></pre>
  <p>DTO of <strong>Doctor</strong></p>
  <pre><code>@Getter
  @Setter
  public class MedecinDTO {
  
  private Long idMedecin;
  @NotBlank(message = &quot;Name does not Null&quot;)
  private String name;
  @Email(message = &quot;Mail not Valid&quot;)
  private String email;
  @NotNull(message = &quot;the doctor must have a speciality. &quot;)
  private String speciality;
  private Collection&lt;RendezVous&gt; rendezVousMedecin;
  }
  </code></pre>
  <p>And the final DTO of <strong>Appointment</strong></p>
  <pre><code>@Data
  public class RendezVousDTO {
  
  private String idRDV;
  private Date dateRDV;
  private StatusRDV status;
  
  private Patient patient;
  
  private Medecin medecin;
  </code></pre>
  <p>}</p>
  <p>And in the service on impliments here is the code of <strong>Update</strong> and <strong>Delete</strong></p>
  <pre><code>@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&lt;String, Boolean&gt; deletePatient(Long id) throws PatientNotFoundException {
      Patient patient = patientRepository.findById(id).orElseThrow(() -&gt; new 
              PatientNotFoundException(&quot;Patient Not Found with id : &quot; + id));
      patientRepository.delete(patient);
      Map&lt;String, Boolean&gt; mapDelete = new HashMap&lt;&gt;();
      mapDelete.put(&quot;Delete Patient&quot;, Boolean.TRUE);
      return mapDelete;
  }
  
  @Override
  public Map&lt;String, Boolean&gt; deleteMedecin(Long id) throws MedecinNotFoundException {
      MedecinDTO medecinDTO = getMedecin(id);
      medecinRepository.delete(medecinMapper.fromMedecinDTO(medecinDTO));
      Map&lt;String, Boolean&gt; mapDelete = new HashMap&lt;&gt;();
      mapDelete.put(&quot;Delete Medecin&quot;, Boolean.TRUE);
      return mapDelete;
  }
  
  @Override
  public Map&lt;String, Boolean&gt; deleteRDV(String id) throws RendezVousNotFound {
      RendezVousDTO rendezVousDTO = findRDVById(id);
      rendezVousRepository.delete(rendezVousMapper.fromRendeVousDTO(rendezVousDTO));
      Map&lt;String, Boolean&gt; mapDelete = new HashMap&lt;&gt;();
      mapDelete.put(&quot;Delete Rendez vous&quot;, Boolean.TRUE);
      return mapDelete;
     }
  }
  </code></pre>
  <p>The problem if I am going to modify one of the DTOs: the attributes of the Patient or doctor it works but the other modifications cannot be modified. For example if I want to modify the attribute name, mail it will work but if I want to modify the Appointment I cannot. And the same for the appointment.
  I tried with POSTMAN and still there is an exception happening with debug</p>
  <blockquote>
  <p>Method threw 'org.hibernate.LazyInitializationException' exception.
  Cannot evaluate com.example.Entities.Patient.toString()</p>
  </blockquote>
  <p>without debug at the intellij console</p>
  <blockquote>
  <p>java.lang.NullPointerException: null</p>
  </blockquote>
  <p>And for deletion If I want to delete a Patient with or without an appointment. The deletion is done successfully. But the problem the patient is deleted in the DB, however the appointment which is related to the patient or the doctor already exists in the DB and with the command of getRDV the exception message is displayed that the appointment does not exist.</p>
  <p>I hope I have explained well and thank you for all who help me.</p>