JPA之ManyToMany的处理

在处理关系中,最为麻烦的可能是多对多的关系处理,因为多对多的关系要要一个中间表来进行维护。但是实际上多对多的关系也是我们比较常见的一种关系。

[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]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值