The @ManyToMany annotation is used to specify a many-to-many database relationship.
In many-to-many association, the source entity has a field that stores a collection of target entities.
A many-to-many association always uses an intermediate join table to store the association that joins two entities. The join table is defined using the @JoinTable JPA annotation.
@ManyToMany - JPA Annotation Example
Consider the following tables where employees and projects exhibit a many-to-many relationship between each other -
The many-to-many relationship is implemented using a third table called employees_projects which contains the details of the employees and their associated projects.Let's create JPA entities that we map with database tables. We use @ManyToMany annotation to create a many-to-many relationship between two entities. In a bi-directional association, the @ManyToMany annotation is used on both the entities but only one entity can be the owner of the relationship.
Employee
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
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.ManyToMany;
import javax.persistence.Table;
@Entity
@Table(name = "employees")
public class Employee {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "employee_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long employeeId;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@ManyToMany(cascade = {
CascadeType.ALL
})
@JoinTable(
name = "employees_projects",
joinColumns = {
@JoinColumn(name = "employee_id")
},
inverseJoinColumns = {
@JoinColumn(name = "project_id")
}
)
Set < Project > projects = new HashSet < Project > ();
public Employee() {
super();
}
public Employee(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public Employee(String firstName, String lastName, Set < Project > projects) {
this.firstName = firstName;
this.lastName = lastName;
this.projects = projects;
}
public Long getEmployeeId() {
return employeeId;
}
public void setEmployeeId(Long employeeId) {
this.employeeId = employeeId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Set < Project > getProjects() {
return projects;
}
public void setProjects(Set < Project > projects) {
this.projects = projects;
}
}
Project
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
@Entity
@Table(name = "projects")
public class Project {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "project_id")
@GeneratedValue
private Long projectId;
@Column(name = "title")
private String title;
@ManyToMany(mappedBy = "projects", cascade = { CascadeType.ALL })
private Set<Employee> employees = new HashSet<Employee>();
public Project() {
super();
}
public Project(String title) {
this.title = title;
}
public Long getProjectId() {
return projectId;
}
public void setProjectId(Long projectId) {
this.projectId = projectId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Set<Employee> getEmployees() {
return employees;
}
public void setEmployees(Set<Employee> employees) {
this.employees = employees;
}
}
Let's understand the JPA annotations used in above JPA entities.
@ManyToMany - The @ManyToMany annotation is used to specify a many-to-many database relationship.
@JoinColumn - The @JoinColumn annotation is used to specify the FOREIGN KEY column used when joining an entity association or an embeddable collection.
@JoinColumns - The @JoinColumns annotation is used to group multiple @JoinColumn annotations, which are used when mapping entity association or an embeddable collection using a composite identifier
@JoinTable - The @JoinTable annotation is used to specify the link table between two other database tables.
Check out the complete tutorial at https://www.javaguides.net/2019/12/hibernate-5-many-to-many-annotation-mapping-example.html
References
https://www.javaguides.net/2019/12/hibernate-5-many-to-many-annotation-mapping-example.html
https://javaee.github.io/javaee-spec/javadocs/javax/persistence/ManyToMany.html
Comments
Post a Comment