in my example here i want to display a list of appointments of a specific patient in a one to many relationships .. the process is going well but the problem is how to display this list which is in patient as an attribute.
Appointment Entity
package ma.ac.ensaf.cabinetmedical.entities;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Entity
@Table(name="APPOINTMENTS")
@Data @AllArgsConstructor @NoArgsConstructor @ToString
public class Appointment {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Temporal(TemporalType.DATE)
private Date dateAppointment;
@ManyToOne
@JoinColumn(name="patient_id", nullable=false)
private PatientEntity patient;
}
Patient Entity
package ma.ac.ensaf.cabinetmedical.entities;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Entity
@Table(name="PATIENTS")
@Data @AllArgsConstructor @NoArgsConstructor @ToString
public class PatientEntity {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name="NOM",length =25)
private String nom;
@Column(name="PRENOM",length =25)
private String prenom;
@Column(name="CIN",length=25)
private String cin;
@Temporal(TemporalType.DATE)
private Date dateNaissance;
@OneToMany(cascade = CascadeType.ALL, fetch= FetchType.LAZY)
@JoinColumn(name = "patient_id",referencedColumnName="id")
private List<Appointment> appointmentList = new ArrayList<Appointment>();
}
Appointment Repository
package ma.ac.ensaf.cabinetmedical.repositories;
import org.springframework.data.jpa.repository.JpaRepository;
import ma.ac.ensaf.cabinetmedical.entities.Appointment;
public interface AppointmentRep extends JpaRepository<Appointment, Integer>{
}
Patient Repository
package ma.ac.ensaf.cabinetmedical.repositories;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import ma.ac.ensaf.cabinetmedical.entities.PatientEntity;
public interface PatientRep extends JpaRepository<PatientEntity,Integer> {
public List<PatientEntity> findByNomContains(String name);
}
Appointment Service
package ma.ac.ensaf.cabinetmedical.services;
import org.springframework.stereotype.Service;
@Service
public class AppointmentService {
}
Patient Service
package ma.ac.ensaf.cabinetmedical.services;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ma.ac.ensaf.cabinetmedical.entities.Appointment;
import ma.ac.ensaf.cabinetmedical.entities.PatientEntity;
import ma.ac.ensaf.cabinetmedical.repositories.AppointmentRep;
import ma.ac.ensaf.cabinetmedical.repositories.PatientRep;
@Service
public class PatientService {
@Autowired
PatientRep patientRepository;
@Autowired
AppointmentRep appointmentRep;
//return a list of patients
public List <PatientEntity> getAllPatientEntity()
{
List<PatientEntity> patients = new ArrayList<PatientEntity>();
patientRepository.findAll().forEach(p->patients.add(p));
return patients;
}
//search a patient by id
public PatientEntity getPatientEntityById(int id)
{
return patientRepository.findById(id).get();
}
// save a new patient
public void saveOrUpdate(PatientEntity patient)
{
patientRepository.save(patient);
}
//delete a specific record
public void delete(int id)
{
patientRepository.deleteById(id);
}
//update a record
public void update(PatientEntity patient, int patienId)
{
patientRepository.save(patient);
}
}
Appointment Controller
package ma.ac.ensaf.cabinetmedical.web;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class AppointmentController {
}
Patient Controller
package ma.ac.ensaf.cabinetmedical.web;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import ma.ac.ensaf.cabinetmedical.entities.PatientEntity;
import ma.ac.ensaf.cabinetmedical.services.PatientService;
@RestController
public class PatientController {
@Autowired
PatientService patientService;
@GetMapping("/patients")
private List<PatientEntity> listPatients()
{
return patientService.getAllPatientEntity();
}
@GetMapping("/patients/{id}")
private PatientEntity monPatient(@PathVariable int id)
{
return patientService.getPatientEntityById(id);
}
@DeleteMapping("/delete/{id}")
private void deletePatient(@PathVariable int id)
{
patientService.delete(id);
}
@PostMapping("/addPatient")
private int savePatient(@RequestBody PatientEntity patient)
{
patientService.saveOrUpdate(patient);
return patient.getId();
}
@PutMapping("/update")
private PatientEntity update(@RequestBody PatientEntity patient)
{
patientService.saveOrUpdate(patient);
return patient;
}
}
Thanks for your help
As you have many to one mapping in Appointment entity. you could write the below query in AppointmentRep
List<Appointment> findAllByPatientId(int id);