Let's say there is an Employee
's which has a collection of Course
.
class Employee {
@OneToMany
private Set<Course> courses.
}
I want something like below
first_name | last_name | course_ids
Jill | Smith. | 1,2,3
Eve | Jackson. | 1,4,5
I am not so advanced in hibernate, can someone please help.
The method that I am about to show to you is the One to many unidirectional method. In your database you should create a table for courses and it must contain employee_id as a foreign key and an id of the course as a primary key. Then, create the Course class as below.(let's assume that the name of the course table in the database is "course":
Notice that the employee_id is not in the course class.Then create the employee class:
You must define getters and setters in every Entity you create because Hibernate uses them to set the fields in the Entity with the value retrieved from the database