Relationships

Relationships among the tables








One to One Relationship

Employee.java

   @OneToOne(fetch = FetchType.LAZY, cascade =  CascadeType.ALL, mappedBy = "employee")
   private UserAccount userAccount;


UserAccount.java

    @OneToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "employee_id", nullable = false)
     private Employee employee;



One to Many Relationship

Admin.java

@OneToMany(mappedBy = "admin", cascade = {CascadeType.PERSIST, CascadeType.MERGE})
private Set<Employee> employees;


Employee.java

@ManyToOne
@JoinColumn(name="admin_id", nullable = false)
 private Admin admin;



Many To Many Relationship

Since the Department is a week entity the Many to Many relationship for the Department table and the Employee table is in the Department.java class.

Department.java

@ManyToMany
@JoinTable( name = "employee_department",
            joinColumns = @JoinColumn(name = "department_id", referencedColumnName = "id"),
            inverseJoinColumns = @JoinColumn(name = "employee_id", referencedColumnName = "id"))
  private Set<Employee> employees;

Post a Comment

0 Comments