在处理关系中,最为麻烦的可能是多对多的关系处理,因为多对多的关系要要一个中间表来进行维护。但是实际上多对多的关系也是我们比较常见的一种关系。
[color=red]业务需求:在学校的选课系统中老师和学生的关系则是一种多对多的关系[/color]
首先还是将老师和学生的实体类编写出来。
测试类
[b]
总结:这里更要注意关系维护端和关系被维护端。
[/b]
[color=red]业务需求:在学校的选课系统中老师和学生的关系则是一种多对多的关系[/color]
首先还是将老师和学生的实体类编写出来。
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>();//用集合类来存放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;
}
@ManyToMany(cascade = CascadeType.REFRESH)
@JoinTable(name = "student_teacher", inverseJoinColumns = @JoinColumn(name = "teacher_id"), joinColumns = @JoinColumn(name = "student_id")) //JoinTable就是定义中间表的名字以及关联字段名
public Set<Teacher> getTeacher() {
return teachers;
}
public void setTeacher(Set<Teacher> teacher) {
this.teachers = teacher;
}
public void setName(String name) {
this.name = name;
}
public void addTeacher(Teacher teacher){
this.teachers.add(teacher);
}
public void removeTeacher(Teacher teacher){
if(this.teachers.contains(teacher)){//判断该学生是否包含了这个老师,如果存在就删除。这里要重新Teacher类中的hashCode和equals方法
this.teachers.remove(teacher);
}
}
}
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;
@Entity
public class Teacher {
private Integer id;
private String name;
private Set<Student> student = 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="teacher")//这里说明了关系维护端是student,teacher是关系被维护端
public Set<Student> getStudent() {
return student;
}
public void setStudent(Set<Student> student) {
this.student = student;
}
@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;
}
}
测试类
package junit.test;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.junit.BeforeClass;
import org.junit.Test;
import cn.itcast.bean.Student;
import cn.itcast.bean.Teacher;
public class ManyToManyTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Test
public void save() {
EntityManagerFactory factory = Persistence
.createEntityManagerFactory("test");
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
em.persist(new Student("Twe"));
em.persist(new Teacher("蒋老师"));
em.getTransaction().commit();
em.close();
factory.close();
}
/**
* 建立学生和老师的关系
*/
@Test
public void buildTS() {
EntityManagerFactory factory = Persistence
.createEntityManagerFactory("test");
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
Student student = em.find(Student.class, 1);
student.addTeacher(em.getReference(Teacher.class, 1));
em.getTransaction().commit();
em.close();
factory.close();
}
/**
* 解除学生和老师的关系
*/
@Test
public void deleteTS() {
EntityManagerFactory factory = Persistence
.createEntityManagerFactory("test");
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
Student student = em.find(Student.class, 1);
student.removeTeacher(em.getReference(Teacher.class, 1));
em.getTransaction().commit();
em.close();
factory.close();
}
/**
* 删除老师
*/
@Test
public void deleteTeacher() {
EntityManagerFactory factory = Persistence
.createEntityManagerFactory("test");
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
Student student = em.find(Student.class, 1);
Teacher teacher = em.getReference(Teacher.class, 1);
student.removeTeacher(teacher);
em.remove(em.getReference(Teacher.class, 1));
em.getTransaction().commit();
em.close();
factory.close();
}
/**
* 删除学生
*/
@Test
public void deleteStudent() {
EntityManagerFactory factory = Persistence
.createEntityManagerFactory("test");
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
Student student = em.getReference(Student.class, 1);
em.remove(student);
em.getTransaction().commit();
em.close();
factory.close();
}
}
[b]
总结:这里更要注意关系维护端和关系被维护端。
[/b]