title: New entity required in an existing ManyToMany
body: <p>I have two entities: <strong>Project.java</strong> and <strong>Employee.java</strong> that are <code>ManyToMany</code> mapped with each other (internally using the Hibernate generated table <strong>emp_project_mapping</strong>).</p>
  <p>Now, I have a new one: <strong>Role.java</strong> 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 <strong>emp_project_mapping</strong>.</p>
  <p>I have no idea how to achieve this scenario. Please help me out.</p>
  <p><strong>Employee.java</strong></p>
  <pre><code>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=&quot;employee_table&quot;)
  //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=&quot;empcode&quot;,nullable=false,unique=true)
      private String empCode;
      
      @OneToOne(mappedBy=&quot;emp&quot;)
      private Workstation workStation;
      
      @ManyToMany
      @JoinTable(name=&quot;emp_project_mapping&quot;,joinColumns=@JoinColumn(name=&quot;emp_id&quot;),inverseJoinColumns=@JoinColumn(name=&quot;project_id&quot;))
      private Set&lt;Project&gt; prj=new HashSet&lt;Project&gt;();
      
      
      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&lt;Project&gt; getProject()
      {
          return this.prj;
      }
      public void setProject(Set&lt;Project&gt; prj)
      {
          this.prj=prj;
      }
      
      @Override
      public String toString() {
          return &quot;Employee [id=&quot; + id + &quot;, code=&quot;+ empCode + &quot;, name=&quot; + name + &quot;, gender=&quot; + gender + &quot;, department=&quot; + department
                  + &quot;, designation=&quot; + designation + &quot;, dob=&quot; + dob + &quot;]&quot;;
      } 
  }
  </code></pre>
  <p><strong>Project.java</strong></p>
  <pre><code>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=&quot;projects_table&quot;)
  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=&quot;prj&quot;)
      private Set&lt;Employee&gt; emp=new HashSet&lt;Employee&gt;();
      
      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&lt;Employee&gt; getEmployee()
      {
          return this.emp;
      }
      public void setEmployee(Set&lt;Employee&gt; emp)
      {
          this.emp=emp;
      }
      
      @Override
      public String toString() {
          return &quot;Project [id=&quot; + id + &quot;, oea_number=&quot; + oea_number + &quot;, project_name=&quot; + project_name + &quot;, client_name=&quot;
                  + client_name + &quot;, origin_country=&quot; + origin_country + &quot;, po_amount=&quot; + po_amount + &quot;]&quot;;
      }
      
      
  }
  </code></pre>
  <p><strong>Role.java</strong></p>
  <pre><code>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=&quot;Employee_roles&quot;)
  public class Role
  {
      @Id
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      private Integer id;
      
      @Column(name=&quot;description&quot;,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;
      }
      
      
  }
  </code></pre>
  <p><em>emp_project_mapping table</em></p>
  <p><a href="https://i.stack.imgur.com/HNdiM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HNdiM.png" alt="enter image description here" /></a></p>