學生和老師就是多對多的關係。一個學生有多個老師,一個老師教多個學生。多對多映射採起中間錶鏈接的映射策略,創建的中間表將分別引入兩邊的主鍵做爲外鍵。jpa 對於中間表的元數據提供了可配置的方式,用戶能夠自定義中間表的表名,列名。java
下面就以學生和老師爲例介紹多對多映射關係的實例開發app
Student實體類 ide
package com.ljq.entity;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
@SuppressWarnings("serial")
@Entity
public class Student implements java.io.Serializable {
/** 學生ID **/
private Integer studentid;
/** 學生姓名 **/
private String name;
private Set<Teacher> teachers=new HashSet<Teacher>();
public Student() {
super();
}
public Student(String name) {
super();
this.name = name;
}
@Id
@GeneratedValue
public Integer getStudentid() {
return studentid;
}
public void setStudentid(Integer studentid) {
this.studentid = studentid;
}
@Column(nullable=false,length=32)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//@ManyToMany註釋表示Student是多對多關係的一邊,mappedBy屬性定義了Student爲雙向關係的維護端
//Teacher表是關係的維護者,owner side,有主導權,它有個外鍵指向Student表。
@ManyToMany(mappedBy = "students")
public Set<Teacher> getTeachers() {
return teachers;
}
public void setTeachers(Set<Teacher> teachers) {
this.teachers = teachers;
}
}
測試
Teacher實體類 fetch
package com.ljq.entity;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
@SuppressWarnings("serial")
@Entity
public class Teacher implements java.io.Serializable {
/** 教師ID **/
private Integer teacherid;
/** 教師姓名 **/
private String name;
private Set<Student> students=new HashSet<Student>();
public Teacher() {
super();
}
public Teacher(String name) {
super();
this.name = name;
}
@Id
@GeneratedValue
public Integer getTeacherid() {
return teacherid;
}
public void setTeacherid(Integer teacherid) {
this.teacherid = teacherid;
}
@Column(nullable=false,length=32)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//@ManyToMany註釋表示Teacher是多對多關係的一端。
//@JoinTable描述了多對多關係的數據表關係。name屬性指定中間表名稱,joinColumns定義中間表與Teacher表的外鍵關係。
//中間表Teacher_Student的Teacher_ID列是Teacher表的主鍵列對應的外鍵列,inverseJoinColumns屬性定義了中間表與另一端(Student)的外鍵關係。
@ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
@JoinTable(name = "Teacher_Student",
joinColumns = { @JoinColumn(name = "Teacher_ID", referencedColumnName = "teacherid") },
inverseJoinColumns = { @JoinColumn(name = "Student_ID", referencedColumnName = "studentid") })
public Set<Student> getStudents() {
return students;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
/**
* 添加學生
*
* @param student
*/
public void addStudent(Student student) {
if (!this.students.contains(student)) {
this.students.add(student);uistudent.setTeacher(this);
}
}
/**
* 刪除學生
*
* @param student
*/
public void removeStudent(Student student) {
if(this.students.contains(student)){thisstudent.setTeacher(null);
this.students.remove(student);
}
}
}
spa
ManyToManyTest測試類 code
package com.ljq.test;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.junit.Test;
import com.ljq.entity.Student;
import com.ljq.entity.Teacher;
public class ManyToManyTest {
@Test
public void save() {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("ljq");
EntityManager em=factory.createEntityManager();
em.getTransaction().begin();
em.persist(new Teacher("張老師"));
em.persist(new Student("小張"));
em.getTransaction().commit();
em.close();
factory.close();
}
/**
* 爲老師添加一個學生
*
*/
@Test
public void build() {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("ljq");
EntityManager em=factory.createEntityManager();
em.getTransaction().begin();
Teacher teacher = em.find(Teacher.class, 2);
teacher.addStudent(em.getReference(Student.class, 2));
em.getTransaction().commit();
em.close();
factory.close();
}
/**
* 解除學生跟老師的關係
*
*/
@Test
public void remove() {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("ljq");
EntityManager em=factory.createEntityManager();
em.getTransaction().begin();
Teacher teacher = em.find(Teacher.class, 2);
teacher.removeStudent(em.getReference(Student.class, 2));
em.getTransaction().commit();
em.close();
factory.close();
}
/**
* 刪除學生,由於學生不是關係維護者,因此須要先手工解除老師與學生的關聯,而後再刪除學生
*
*/
@Test
public void deleteStudent() {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("ljq");
EntityManager em=factory.createEntityManager();
em.getTransaction().begin();
Teacher teacher = em.find(Teacher.class, 2);
Student student = em.getReference(Student.class, 2);
teacher.removeStudent(student); //手工解除老師與學生的關係
em.remove(student); //刪除學生
em.getTransaction().commit();
em.close();
factory.close();
}
/**
* 刪除老師,由於老師是關係維護者,因此能夠直接解除老師與學生的關係,不用咱們手工解除
*
*/
@Test
public void deleteTeacher() {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("ljq");
EntityManager em=factory.createEntityManager();
em.getTransaction().begin();
em.remove(em.getReference(Teacher.class, 3));
em.getTransaction().commit();
em.close();
factory.close();
}
/**
* 用來判斷映射是否成功
*
*/
@Test
public void test() {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("ljq");
factory.close();
}
}開發