I have two entities: Project.java and Employee.java that are ManyToMany mapped with each other (internally using the Hibernate generated table emp_project_mapping).
Now, I have a new one: Role.java that is to be mapped with above mentioned entities such that an employee in a project can have only one role. Also, this mapping should use that same mapping_table emp_project_mapping.
I have no idea how to achieve this scenario. Please help me out.
Employee.java
package com.ayush.springbootApp.bootCrudApi.model;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import com.ayush.springbootApp.bootCrudApi.model.Project;
import com.ayush.springbootApp.bootCrudApi.model.Workstation;
@Entity
@Table (name="employee_table")
//model class for Employee
public class Employee {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column
private Integer id;
@Column
private String name;
@Column
private String gender;
@Column
private String department;
@Column
private String designation;
@Column
private Date dob;
@Column(name="empcode",nullable=false,unique=true)
private String empCode;
@OneToOne(mappedBy="emp")
private Workstation workStation;
@ManyToMany
@JoinTable(name="emp_project_mapping",joinColumns=@JoinColumn(name="emp_id"),inverseJoinColumns=@JoinColumn(name="project_id"))
private Set<Project> prj=new HashSet<Project>();
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
public String getEmpCode() {
return empCode;
}
public void setEmpCode(String empCode) {
this.empCode = empCode;
}
public Set<Project> getProject()
{
return this.prj;
}
public void setProject(Set<Project> prj)
{
this.prj=prj;
}
@Override
public String toString() {
return "Employee [id=" + id + ", code="+ empCode + ", name=" + name + ", gender=" + gender + ", department=" + department
+ ", designation=" + designation + ", dob=" + dob + "]";
}
}
Project.java
package com.ayush.springbootApp.bootCrudApi.model;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import com.ayush.springbootApp.bootCrudApi.model.Employee;
@Entity
@Table(name="projects_table")
public class Project{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column
private Integer id;
@Column(nullable=false)
private String oea_number;
@Column(nullable=false)
private String project_name;
@Column
private String client_name;
@Column
private String origin_country;
@Column(nullable=false)
private Integer po_amount;
@Column
private int project_active;
@ManyToMany(mappedBy="prj")
private Set<Employee> emp=new HashSet<Employee>();
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getOea_number() {
return oea_number;
}
public void setOea_number(String oea_number) {
this.oea_number = oea_number;
}
public String getProject_name() {
return project_name;
}
public void setProject_name(String project_name) {
this.project_name = project_name;
}
public String getClient_name() {
return client_name;
}
public void setClient_name(String client_name) {
this.client_name = client_name;
}
public String getOrigin_country() {
return origin_country;
}
public void setOrigin_country(String origin_country) {
this.origin_country = origin_country;
}
public Integer getPo_amount() {
return po_amount;
}
public void setPo_amount(Integer po_amount) {
this.po_amount = po_amount;
}
public int getProject_active()
{
return this.project_active;
}
public void setProject_active(int project_active)
{
this.project_active=project_active;
}
public Set<Employee> getEmployee()
{
return this.emp;
}
public void setEmployee(Set<Employee> emp)
{
this.emp=emp;
}
@Override
public String toString() {
return "Project [id=" + id + ", oea_number=" + oea_number + ", project_name=" + project_name + ", client_name="
+ client_name + ", origin_country=" + origin_country + ", po_amount=" + po_amount + "]";
}
}
Role.java
package com.ayush.springbootApp.bootCrudApi.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name="Employee_roles")
public class Role
{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@Column(name="description",nullable=false)
private String desc;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
emp_project_mapping table
