Java Tutorial/JPA/Many To Many Mapping
Содержание
Bidirectional Many To Many Mapping
File: Department.java
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 Department {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private String name;
@ManyToMany(mappedBy="departments")
private Collection<Student> students;
public Department(){
students = new ArrayList<Student>();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String deptName) {
this.name = deptName;
}
public void addStudent(Student student) {
if (!getStudents().contains(student)) {
getStudents().add(student);
}
if (!student.getDepartments().contains(this)) {
student.getDepartments().add(this);
}
}
public Collection<Student> getStudents() {
return students;
}
public void setStudent(Collection<Student> students) {
this.students = students;
}
public String toString() {
return "Department id: " + getId() +
", name: " + getName();
}
}
JPA Creates Join Table For Many To Many Mapping
File: Student.java
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 Student {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private String name;
@ManyToMany
private Collection<Department> departments;
public Student() {
departments = new ArrayList<Department>();
}
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 void addDepartment(Department department) {
if (!getDepartments().contains(department)) {
getDepartments().add(department);
}
if (!department.getStudents().contains(this)) {
department.getStudents().add(this);
}
}
public Collection<Department> getDepartments() {
return departments;
}
public void setDepartment(Collection<Department> departments) {
this.departments = departments;
}
public String toString() {
return "\n\nID:" + id + "\nName:" + name ;
}
}
Many To Many based on Collection
File: Project.java
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
@Entity
public class Project {
@Id
protected int id;
protected String name;
@ManyToMany(mappedBy="projects")
private Collection<Student> students;
public Project() {
students = new ArrayList<Student>();
}
public void setId(int i){
id = i;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public Collection<Student> getStudents() {
return students;
}
public String toString() {
return "Project id: " + getId() + ", name: " + getName();
}
}
Many To Many Join Table Join Inverse Join Column
File: Student.java
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.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
@Entity
public class Student {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private String name;
@ManyToMany
@JoinTable(name="Student_Dept",
joinColumns=@JoinColumn(name="Stut_ID"),
inverseJoinColumns=@JoinColumn(name="DEPT_ID"))
private Collection<Department> departments;
public Student() {
departments = new ArrayList<Department>();
}
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 void addDepartment(Department department) {
if (!getDepartments().contains(department)) {
getDepartments().add(department);
}
if (!department.getStudents().contains(this)) {
department.getStudents().add(this);
}
}
public Collection<Department> getDepartments() {
return departments;
}
public void setDepartment(Collection<Department> departments) {
this.departments = departments;
}
public String toString() {
return "\n\nID:" + id + "\nName:" + name ;
}
}
Many To Many Mapping and Remove Operation
File: Project.java
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
@Entity
public class Project {
@Id
protected int id;
protected String name;
@ManyToMany(mappedBy="projects")
private Collection<Student> students;
public Project() {
students = new ArrayList<Student>();
}
public void setId(int i){
id = i;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public Collection<Student> getStudents() {
return students;
}
public String toString() {
return "Project id: " + getId() + ", name: " + getName();
}
}