One to Many relationship

Other topics

One To Many relationship

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. enter image description here

There are two points to remember about jpa one to many mapping:

  • The many to one side is the owning side of relationship. The column is defined on that side.
  • The one to many mapping is the inverse side side so the mappedBy element must be used on the inverse side.

Complete example can be referred here

Parameters:

AnnotationPurpose
@TableGeneratorSpecifies generator name and table name where generator can be found
@GeneratedValueSpecifies generation strategy and refers to name of generator
@ManyToOneSpecifies many to one relationship between Employee and Department
@OneToMany(mappedBy="department")creates bi-directional relationship between Employee and Department by simply referring to @ManyToOne annotation in Employee entity

Contributors

Topic Id: 6529

Example Ids: 22378

This site is not affiliated with any of the contributors.