Hello, I'm new to thymeleaf. I've two entities,
Patient Doctor which have one-to-many relationship between them, when I tried to set one Doctor to Patient, its showing null. I'm sharing my entities and controller here. please give advice.
Doctor entity
<pre>
@Entity
public class Doctor {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String name;
private String eMail;
private String password;
private String degree;
private String specialization;
Type type = Type.DOCTOR;
@OneToMany(mappedBy="doctor")
private Set<Patient> patients = new HashSet<>();
// all constructors getters,setters and toString()
Patient entity
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String name;
private String eMail;
private String password;
private String bloodgroup;
private String record;
private int age;
Type type = Type.PATIENT;
@ManyToOne
@JoinColumn(name = "doctor_id")
private Doctor doctor;
Controller
@GetMapping("/detailsDoctor")
public String detailsDoctor(@ModelAttribute Doctor doctor,Model model) {
model.addAttribute("doctor", docRep.getOne(doctor.getId()+1));
return "Booking-Appointment-Pages/Booking_Page";
}
@GetMapping("/bookedDoctor")
public String bookedDoctor(@ModelAttribute Doctor doctor, @ModelAttribute Patient patient, Model model) {
model.addAttribute("patient", patient);
if((patient.getDoctor() == null)) {
patient.setDoctor(docRep.getOne(doctor.getId()+1));
}
return "Booking-Appointment-Pages/Confirmed_Booking_Page";
}
What I have tried:
I've checked mapping, it creates table as it has to. but unable to insert the value in it.
I'm so lost right now as it does'nt make any error.