Skip to content
Advertisement

Spring boot delete object from database based on a OneToOne relationship

i’ve got an “appointment” entity object(table) AND dates_doctor_available entity build like this

@Entity
public class Appointment {
.....
 @OneToOne
 @JoinColumn(name = "date_time_available_fk", nullable=true)
 private DatesDoctorAvailable datesDoctorAvailable;
 ......

AND

@Entity
  public class DatesDoctorAvailable {
   ....
  @OneToOne(cascade = CascadeType.ALL, mappedBy="datesDoctorAvailable")
  private Appointment appointment;
  ....

now i wish from a controller to remove the Database entry based on the “date_time_available_fk” Using “Appointment” object. For example something like

     @RequestMapping(value="/show/scheduled/{id}" ,method = RequestMethod.GET)
       public String gettAppointmentDel(Model model, @PathVariable String id, Principal 
       principal, 
       @ModelAttribute("appointment") Appointment appointment, 
       @ModelAttribute("datess.dateID") 
      DatesDoctorAvailable datesDoctoravailable) {
       ......
      //this part below i want to make it work
      appointmentService.deleteByDatesDoctorAvailable(
       appointment.getDatesDoctorAvailable(
        datesDoctorAvailableService.findByDateID(Long.parseLong(id))));
        .....
     }

where inside service there is a data object access something like this(they are interfaces)

@Override
public Appointment deleteByDatesDoctorAvailable(DatesDoctorAvailable 
datesDoctorAvailable) {
    return  
    appointmentDao.deleteByDatesDoctorAvailable(datesDoctorAvailable);

}

where id is the date_time_available_fk number i want to pass(i already have it from thymeleaf) to the Appointment object

i want to avoid using @Query if this can happen.

UPDATE i actually figured it out i will give the solution in the morning for future troublers.

Advertisement

Answer

The Solution i could not fetch the object information from database with above as i said in order to delete it afterwards so i did this(created new object Appointment)

   Appointment appointmentFind = 
   appointmentService.findByDatesDoctorAvailable(
   datesDoctorAvailableService.findByDateID(Long.parseLong(id)));

that way i could retrieve the information then delete it like this.

   appointmentService.deleteScheduleById(appointmentFind.getId()); 

after getting id from url request mapping

@RequestMapping(value="/show/scheduled/{id}" ,method = 
RequestMethod.GET)
public String getAppointmentDel(Model model, @PathVariable String id, 
Principal principal, @ModelAttribute("appointment") Appointment 
appointment, @ModelAttribute("datess.dateID") DatesDoctorAvailable 
datesDoctoravailable) {

—————inside view thymeleaf————

  <td><button type="button"  
  th:onclick=
 "|window.location.href='/appointment/show/scheduled/${datess.dateID}'|" 
  th-text="(id1=${datess.dateID})" class="btn btn-danger btn-xs">
  Cancel it
 </button></td>

——————-inside Services—————

—Service:—

 @Override
  public Appointment findByDatesDoctorAvailable(DatesDoctorAvailable 
   datesDoctorAvailable) {
    return 
       appointmentDao.findByDatesDoctorAvailable(datesDoctorAvailable);
}

—DAO:—–

     Appointment findByDatesDoctorAvailable(DatesDoctorAvailable 
     datesDoctorAvailable);

—Service:—

    void deleteScheduleById(@Param("id") Long id);
    @Override
    public void deleteScheduleById(Long id) {
    appointmentDao.deleteScheduleById(id);  
   }

—DAO:—–

@Modifying
@Transactional
@Query("delete from Appointment where id = :id")
void deleteScheduleById(@Param("id") Long id);
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement