One to Many mapping is generally simply a bidirectional relationship of Many to One mapping. We will take same example that we took for Many to one mapping.
Employee.java
@Entity
public class Employee {
@TableGenerator(name = "employee_gen", table = "id_gen", pkColumnName = "gen_name", valueColumnName = "gen_val", allocationSize = 100)
@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "employee_gen")
private int idemployee;
private String firstname;
private String lastname;
private String email;
@ManyToOne
@JoinColumn(name = "iddepartment")
private Department department;
// getters and setters
}
Department.java
@Entity
public class Department {
@TableGenerator(table = "id_gen", pkColumnName = "gen_name", valueColumnName = "gen_val", name = "department_gen", allocationSize = 1)
@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "department_gen")
private int iddepartment;
private String name;
@OneToMany(mappedBy = "department")
private List<Employee> employees;
// getters and setters
}
This relationship is represented in database as below.
There are two points to remember about jpa one to many mapping:
Complete example can be referred here