首页 > 代码库 > JPA--多对多关系

JPA--多对多关系

JPA中,多对多关系用@ManyToMany标示。

关系维护端:

 1 package com.yl.demo1.bean.manyTomany; 2  3 import java.util.HashSet; 4 import java.util.Set; 5  6 import javax.persistence.CascadeType; 7 import javax.persistence.Column; 8 import javax.persistence.Entity; 9 import javax.persistence.GeneratedValue;10 import javax.persistence.Id;11 import javax.persistence.JoinColumn;12 import javax.persistence.JoinTable;13 import javax.persistence.ManyToMany;14 15 @Entity16 public class Student {17     private Integer id;18     private String name;19     private Set<Teacher> teachers = new HashSet<Teacher>();20     21     22     public Student(){}23     24     public Student(String name) {25         super();26         this.name = name;27     }28     @Id @GeneratedValue29     public Integer getId() {30         return id;31     }32     public void setId(Integer id) {33         this.id = id;34     }35     @Column(length=10, nullable=false)36     public String getName() {37         return name;38     }39     public void setName(String name) {40         this.name = name;41     }42     /**43      * Student是关系的维护端44      * inverseJoinColumns和joinColumns是定义关联表中的字段,其中inverseJoinColumns定义关系被维护端的字段45      * joinColumns定义关系维护端的字段46      */47     @ManyToMany(cascade=CascadeType.REFRESH)48     @JoinTable(name="student_teacher", inverseJoinColumns=@JoinColumn(name="teacher_id"),49     joinColumns=@JoinColumn(name="student_id"))50     public Set<Teacher> getTeachers() {51         return teachers;52     }53     public void setTeachers(Set<Teacher> teachers) {54         this.teachers = teachers;55     }56     57     public void addTeacher(Teacher teacher) {58         this.teachers.add(teacher);59     }60     61     public void removeTeacher(Teacher teacher) {62         if (this.teachers.contains(teacher)) {63             this.teachers.remove(teacher);64         }65     }66 }

 

关系被维护端:

 1 package com.yl.demo1.bean.manyTomany; 2  3 import java.util.HashSet; 4 import java.util.Set; 5  6 import javax.persistence.CascadeType; 7 import javax.persistence.Column; 8 import javax.persistence.Entity; 9 import javax.persistence.FetchType;10 import javax.persistence.GeneratedValue;11 import javax.persistence.Id;12 import javax.persistence.ManyToMany;13 14 @Entity15 public class Teacher {16     private Integer id;17     private String name;18     private Set<Student> students = new HashSet<Student>();19     20     public Teacher(){}21     22     public Teacher(String name) {23         super();24         this.name = name;25     }26     @Id @GeneratedValue27     public Integer getId() {28         return id;29     }30     public void setId(Integer id) {31         this.id = id;32     }33     @Column(length=10, nullable=false)34     public String getName() {35         return name;36     }37     public void setName(String name) {38         this.name = name;39     }40     //teacher为关系的被维护端41     @ManyToMany(cascade=CascadeType.REFRESH, mappedBy="teachers", fetch=FetchType.LAZY)42     public Set<Student> getStudents() {43         return students;44     }45     public void setStudents(Set<Student> students) {46         this.students = students;47     }48 49     @Override50     public int hashCode() {51         final int prime = 31;52         int result = 1;53         result = prime * result + ((id == null) ? 0 : id.hashCode());54         return result;55     }56 57     @Override58     public boolean equals(Object obj) {59         if (this == obj)60             return true;61         if (obj == null)62             return false;63         if (getClass() != obj.getClass())64             return false;65         Teacher other = (Teacher) obj;66         if (id == null) {67             if (other.id != null)68                 return false;69         } else if (!id.equals(other.id))70             return false;71         return true;72     }73     74     75     76     77 }

常见操作:

 1 @Test 2     public void save() { 3         EntityManagerFactory factory = Persistence.createEntityManagerFactory("YL"); 4         EntityManager em = factory.createEntityManager(); 5         em.getTransaction().begin();//事务开始 6          7         Student student = new Student("yyyy"); 8         em.persist(student); 9         10         Teacher teacher = new Teacher("jiawenhui");11         em.persist(teacher);12         13         em.getTransaction().commit();14         em.close();15         factory.close();16     }17 18     /**19      * 建立学生与老师之间的关系20      */21     @Test22     public void buildConnection() {23         EntityManagerFactory factory = Persistence.createEntityManagerFactory("YL");24         EntityManager em = factory.createEntityManager();25         em.getTransaction().begin();//事务开始26         27         Student student = em.find(Student.class, 1);28         29         Teacher teacher = em.getReference(Teacher.class, 1);30         31         student.addTeacher(teacher);32         33         em.getTransaction().commit();34         em.close();35         factory.close();36     }37 38     /**39      * 解除学生与老师之间的关系40      */41     @Test42     public void deleteConnection() {43         EntityManagerFactory factory = Persistence.createEntityManagerFactory("YL");44         EntityManager em = factory.createEntityManager();45         em.getTransaction().begin();//事务开始46         47         Student student = em.find(Student.class, 1);48         49         Teacher teacher = em.getReference(Teacher.class, 1);50         51         student.removeTeacher(teacher);52         53         em.getTransaction().commit();54         em.close();55         factory.close();56     }57     58     /**59      * 删除老师(删除关系被维护端)60      */61     @Test62     public void deleteTeacher() {63         EntityManagerFactory factory = Persistence.createEntityManagerFactory("YL");64         EntityManager em = factory.createEntityManager();65         em.getTransaction().begin();//事务开始66         67         Student student = em.find(Student.class, 1);68         Teacher teacher = em.getReference(Teacher.class, 1);69         //先解除Student和Teacher之间的关系70         student.removeTeacher(teacher);71         //在删除Teacher72         em.remove(teacher);73         74         em.getTransaction().commit();75         em.close();76         factory.close();77     }78     79     /**80      * 删除学生(删除关系维护端)81      */82     @Test83     public void deleteStudent() {84         EntityManagerFactory factory = Persistence.createEntityManagerFactory("YL");85         EntityManager em = factory.createEntityManager();86         em.getTransaction().begin();//事务开始87         88         Student student = em.getReference(Student.class, 1);89         em.remove(student);90         91         em.getTransaction().commit();92         em.close();93         factory.close();94     }95     

 

JPA--多对多关系