Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Java
<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
Java
	@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;
// all constructors getters,setters and toString()


Controller
Java
/*
 * Get Doctor Details
 * */
@GetMapping("/detailsDoctor")
public String detailsDoctor(@ModelAttribute Doctor doctor,Model model) {
    model.addAttribute("doctor", docRep.getOne(doctor.getId()+1));
    return "Booking-Appointment-Pages/Booking_Page";
}

/*
 * Book Doctor
 * */
@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.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900