关于JPA多对多关系,这是使用学生与教师来表示。一个Student由多个Teacher教,同样一个Teacher也可以教多个学生。Student类如下:

 package com.yichun.bean;

 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.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany; //关系的维护端
@Entity
public class Student {
private Integer id;
private String name;
private Set<Teacher> teachers = new HashSet<Teacher>(); public Student() {
} public Student(String name) {
this.name = name;
} @Id
@GeneratedValue
public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} @Column(length = 10, nullable = false)
public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} @ManyToMany(cascade = CascadeType.REFRESH)
// 关联表,inverseJoinColumns被维护端,
@JoinTable(name = "student_teacher", inverseJoinColumns = @JoinColumn(name = "teacher_id"), joinColumns = @JoinColumn(name = "student_id"))
public Set<Teacher> getTeachers() {
return teachers;
} public void setTeachers(Set<Teacher> teachers) {
this.teachers = teachers;
} // 添加teacher与student关系
public void addTeacher(Teacher teacher) {
this.teachers.add(teacher);
} // 删除teacher与student关联
public void removeTeacher(Teacher teacher) {
if (teachers.contains(teacher))
this.teachers.remove(teacher);
} }

Teacher类如下:

 package com.yichun.bean;

 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; //关系被维护端,中间表的记录,不能由teacher删除,
@Entity
public class Teacher {
private Integer id;
private String name;
private Set<Student> students = new HashSet<Student>(); public Teacher() {
} public Teacher(String name) {
this.name = name;
} @Id
@GeneratedValue
public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} @Column(length = 10, nullable = false)
public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} // 多对多基本不用级联删除
@ManyToMany(cascade = CascadeType.REFRESH, mappedBy = "teachers")
public Set<Student> getStudents() {
return students;
} public void setStudents(Set<Student> students) {
this.students = students;
} // id不为null返回id的hashCode,若没有id的时候,hashCode为0
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
} @Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Teacher other = (Teacher) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
} }

  这里Student是作为关系维护端,而Teacher是作为关系被维护端。所以当删除教师的时候,必须解除学生和教师的关联关系。代码如下:

     /**
*<p>
* 删除老师
* </p>
* 解除学生和老师的关系
*/
@Test
public void deleteTeacher() {
EntityManagerFactory factory = Persistence
.createEntityManagerFactory("yichun");
EntityManager manager = factory.createEntityManager();
manager.getTransaction().begin(); // 解除关系
Student student = manager.find(Student.class, 1);
Teacher teacher = manager.getReference(Teacher.class, 1);
student.removeTeacher(teacher); // 删除教师
manager.remove(teacher); manager.getTransaction().commit();
manager.close();
factory.close();
}

  而删除学生的时候,可以直接删除,jpa会自动解除学生与教师的关联关系,然后删除学生。

     /**
*<p>
* 删除学生
* </p>
* 直接可以删除,hibernate会自动解除学生和老师的关系,之后删除学生
*/
@Test
public void deleteStudent() {
EntityManagerFactory factory = Persistence
.createEntityManagerFactory("yichun");
EntityManager manager = factory.createEntityManager();
manager.getTransaction().begin(); // 删除学生
Student student = manager.find(Student.class, 1);
manager.remove(student); manager.getTransaction().commit();
manager.close();
factory.close();
}
04-30 13:02