JPA基础(十二):JPA中的多对多关系

Student.java:

 1 @Entity
2 public class Student {
3 @Id
4 @GeneratedValue
5 private Integer id;
6 @Column(length=10,nullable=false)
7 private String name;
8 @ManyToMany
9 @JoinTable(name="student_teacher",joinColumns=@JoinColumn(name="studentId", referencedColumnName="id"),
10 inverseJoinColumns=@JoinColumn(name="teacherId", referencedColumnName="id"))
11 private Set<Teacher> teachers=new HashSet<Teacher>();
12 /*假如不对关联表里的字段做任何设定,那么表里面的字段默认由JPA的实现产品来帮我们自动生成。
13 inverseJoinColumns:inverse中文是反转的意思,但是觉得太恶心了,在JPA里,可以理解为被维护端。
14 inverseJoinColumns:被维护端外键的定义。
15 @JoinColumn:外键,设置中间表跟teacher表的主键关联的那个外键的名称。
16 joinColumns:关系维护端的定义。*/
17 public Student(){}
18 public Student(String name) {
19 super();
20 this.name = name;
21 }
22 public Integer getId() {
23 return id;
24 }
25 public void setId(Integer id) {
26 this.id = id;
27 }
28 public String getName() {
29 return name;
30 }
31 public void setName(String name) {
32 this.name = name;
33 }
34 public Set<Teacher> getTeachers() {
35 return teachers;
36 }
37 public void setTeachers(Set<Teacher> teachers) {
38 this.teachers = teachers;
39 }
40
41 }

Teacher.java:

 1 @Entity
2 public class Teacher {
3 @Id
4 @GeneratedValue
5 private Integer id;
6 @Column(length=10,nullable=false)
7 private String name;
8 @ManyToMany(mappedBy="teachers")//关系交给Student端维护
9 private Set<Student> students=new HashSet<Student>();
10 public Teacher(){}
11 public Teacher(String name) {
12 super();
13 this.name = name;
14 }
15 public Integer getId() {
16 return id;
17 }
18 public void setId(Integer id) {
19 this.id = id;
20 }
21 public String getName() {
22 return name;
23 }
24 public void setName(String name) {
25 this.name = name;
26 }
27 public Set<Student> getStudents() {
28 return students;
29 }
30 public void setStudents(Set<Student> students) {
31 this.students = students;
32 }
33
34 }

测试:

 1     @Test
2 public void save(){//存储实体
3 EntityManagerFactory factory=Persistence.createEntityManagerFactory("sample");
4 EntityManager em=factory.createEntityManager();
5 em.getTransaction().begin();
6 em.persist(new Teacher("teacher"));
7 em.persist(new Student("student"));
8 em.getTransaction().commit();
9 em.close();
10 factory.close();
11 }
12 @Test
13 public void buildRelation(){//建立实体间关系
14 EntityManagerFactory factory=Persistence.createEntityManagerFactory("sample");
15 EntityManager em=factory.createEntityManager();
16 em.getTransaction().begin();
17 Student student=em.find(Student.class, 1);
18 Teacher teacher=em.getReference(Teacher.class, 1);//getReference延迟加载,可提高性能
19 student.getTeachers().add(teacher);
20 em.getTransaction().commit();
21 em.close();
22 factory.close();
23 }
24 @Test
25 public void deleteRelation(){//解除实体间关系
26 EntityManagerFactory factory=Persistence.createEntityManagerFactory("sample");
27 EntityManager em=factory.createEntityManager();
28 em.getTransaction().begin();
29 Student student=em.find(Student.class, 1);
30 Teacher teacher=em.getReference(Teacher.class, 1);//getReference延迟加载,可提高性能
31 student.getTeachers().remove(teacher);
32 em.getTransaction().commit();
33 em.close();
34 factory.close();
35 }
36 @Test
37 public void deleteTeacher(){//删除老师,必须先删除中间表中得关系,才能删除老师
38 EntityManagerFactory factory=Persistence.createEntityManagerFactory("sample");
39 EntityManager em=factory.createEntityManager();
40 em.getTransaction().begin();
41 Student student=em.find(Student.class, 1);
42 Teacher teacher=em.getReference(Teacher.class, 1);//getReference延迟加载,可提高性能
43 student.getTeachers().remove(teacher);
44 em.remove(teacher);
45 em.getTransaction().commit();
46 em.close();
47 factory.close();
48 }
49 @Test
50 public void deleteStudent(){//删除学生,因为学生端是关系维护端,不用手动删除中间表的关系,会自动删除
51 EntityManagerFactory factory=Persistence.createEntityManagerFactory("sample");
52 EntityManager em=factory.createEntityManager();
53 em.getTransaction().begin();
54 Student student=em.find(Student.class, 1);
55 em.remove(student);
56 em.getTransaction().commit();
57 em.close();
58 factory.close();
59 }

双向多对多关系是一种对等关系,既然是对等关系,也就是说我们可以人为决定谁是关系维护端,实际中根据业务需要自行选择。



posted @ 2011-12-03 13:12  一直在等  阅读(2062)  评论(0编辑  收藏  举报