package cn.itcast.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;
}
@ManyToMany(cascade=CascadeType.REFRESH)
@JoinTable(name="student_teacher",inverseJoinColumns=@JoinColumn(name="teahcer_id"),joinColumns=@JoinColumn(name="student_id"))
public Set<Teacher> getTeachers() {
return teachers;
}
public void setTeachers(Set<Teacher> teachers) {
this.teachers = teachers;
}
public void addTeachers(Teacher teacher){
this.teachers.add(teacher);
}
public void removeTeahcer(Teacher teacher){
if(this.teachers.contains(teacher)){
this.teachers.remove(teacher);
}
}
@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;
}
}
-----------------------------------------------------------------------------------------------------------------------------------------
package cn.itcast.bean;
import java.util.Set;
import java.util.HashSet;
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;
}
@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;
}
@ManyToMany(cascade=CascadeType.REFRESH,mappedBy="teachers")
public Set<Student> getStudent() {
return student;
}
public void setStudent(Set<Student> student) {
this.student = student;
}
@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;
}
}
------------------------------------------------------------------------------------------------
junit
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("itcast");
EntityManager em=factory.createEntityManager();
em.getTransaction().begin();
em.persist(new Student("小张"));
em.persist(new Teacher("李勇老师"));
em.getTransaction().commit();
em.close();
factory.close();
}
/**
* 建立学生跟老师的关系
*/
@Test public void buildTS(){
EntityManagerFactory factory=Persistence.createEntityManagerFactory("itcast");
EntityManager em=factory.createEntityManager();
em.getTransaction().begin();
Student student=em.find(Student.class,1);
student.addTeachers(em.getReference(Teacher.class,1));
em.getTransaction().commit();
em.close();
factory.close();
}
/**
* 解除学生跟老师的关系
*/
@Test public void deleteTS(){
EntityManagerFactory factory=Persistence.createEntityManagerFactory("itcast");
EntityManager em=factory.createEntityManager();
em.getTransaction().begin();
Student student=em.find(Student.class,1);
student.removeTeahcer(em.getReference(Teacher.class,1));
em.getTransaction().commit();
em.close();
factory.close();
}
/**
* 删除老师
*/
@Test public void deleteTeacher(){
EntityManagerFactory factory=Persistence.createEntityManagerFactory("itcast");
EntityManager em=factory.createEntityManager();
em.getTransaction().begin();
Student student=em.find(Student.class,1);
Teacher teacher=em.getReference(Teacher.class,1);
student.removeTeahcer(teacher);
em.remove(teacher);
em.getTransaction().commit();
em.close();
factory.close();
}
/**
* 删除学生
*/
@Test public void deleteStudent(){
EntityManagerFactory factory=Persistence.createEntityManagerFactory("itcast");
EntityManager em=factory.createEntityManager();
em.getTransaction().begin();
Student student=em.getReference(Student.class,1);
em.remove(student);
em.getTransaction().commit();
em.close();
factory.close();
}
}