The following code shows how to create a bidirectional many to many mapping.
The following code is from Professor.java.
package com.java2s.common; import java.util.ArrayList; import java.util.Collection; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.ManyToMany; @Entity public class Professor { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; private String name; private long salary; @ManyToMany private Collection<Project> projects; public Professor() { projects = new ArrayList<Project>(); } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public long getSalary() { return salary; } public void setSalary(long salary) { this.salary = salary; } public void addProject(Project project) { if (!getProjects().contains(project)) { getProjects().add(project); } if (!project.getEmployees().contains(this)) { project.getEmployees().add(this); } } public Collection<Project> getProjects() { return projects; } public String toString() { return "Employee id: " + getId() + " name: " + getName() + " with " + getProjects().size() + " projects"; } }
The following code is from PersonDaoImpl.java.
package com.java2s.common; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.springframework.transaction.annotation.Transactional; @Transactional public class PersonDaoImpl { public void test() { Professor emp = new Professor(); emp.setName("name"); emp.setSalary(12345); Project p = new Project(); p.setName("Chem"); p.getEmployees().add(emp); emp.getProjects().add(p); em.persist(p); em.persist(emp); } @PersistenceContext private EntityManager em; }
The following code is from Project.java.
package com.java2s.common; import java.util.ArrayList; import java.util.Collection; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.ManyToMany; @Entity public class Project { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) protected int id; protected String name; @ManyToMany(mappedBy="projects") private Collection<Professor> employees; public Project() { employees = new ArrayList<Professor>(); } public int getId() { return id; } public void setId(int projectNo) { this.id = projectNo; } public String getName() { return name; } public void setName(String projectName) { this.name = projectName; } public Collection<Professor> getEmployees() { return employees; } public void addEmployee(Professor employee) { if (!getEmployees().contains(employee)) { getEmployees().add(employee); } if (!employee.getProjects().contains(this)) { employee.getProjects().add(this); } } public String toString() { return "Project id: " + getId() + ", name: " + getName() + " with " + getEmployees().size() + " employees"; } }
The following is the database dump.
Table Name: PROFESSOR Row: Column Name: ID, Column Type: INTEGER: Column Value: 1 Column Name: NAME, Column Type: VARCHAR: Column Value: name Column Name: SALARY, Column Type: BIGINT: Column Value: 12345 Table Name: PROFESSOR_PROJECT Row: Column Name: EMPLOYEES_ID, Column Type: INTEGER: Column Value: 1 Column Name: PROJECTS_ID, Column Type: INTEGER: Column Value: 1 Table Name: PROJECT Row: Column Name: ID, Column Type: INTEGER: Column Value: 1 Column Name: NAME, Column Type: VARCHAR: Column Value: Chem