hibernate学习六(关系映射多对多)

一、多对多

  多对多:在学生与老师的情况下,一个老师可以教多个学生,一个学生可以被多个老师教;

  对于类:某一方拥有另一方的一个集合类

二、修改student.java和teacher.java类(单向)

  单向:老师知道自己教哪些学生,学生不知道教自己的老师

  复制05工程,并更名为Hibernate_06_ManyToMany;

 1 package com.model;
 2 
 3 import java.util.HashSet;
 4 import java.util.Set;
 5 
 6 import javax.persistence.Entity;
 7 import javax.persistence.GeneratedValue;
 8 import javax.persistence.Id;
 9 import javax.persistence.ManyToMany;
10 import javax.persistence.OneToMany;
11 import javax.persistence.Table;
12 
13 @Entity
14 @Table(name="teacher")
15 public class Teacher {
16     private int id;
17     private String teacherName;
18     private int teacherAge;
19     private String title;
20     private Set<Student> students = new HashSet<Student>();
21     
22     @Id
23     @GeneratedValue
24     public int getId() {
25         return id;
26     }
27     public void setId(int id) {
28         this.id = id;
29     }
30     public String getTeacherName() {
31         return teacherName;
32     }
33     public void setTeacherName(String teacherName) {
34         this.teacherName = teacherName;
35     }
36     public int getTeacherAge() {
37         return teacherAge;
38     }
39     public void setTeacherAge(int teacherAge) {
40         this.teacherAge = teacherAge;
41     }
42     public String getTitle() {
43         return title;
44     }
45     public void setTitle(String title) {
46         this.title = title;
47     }
48     @ManyToMany
49     public Set<Student> getStudents() {
50         return students;
51     }
52     public void setStudents(Set<Student> students) {
53         this.students = students;
54     }
55 }
 1 package com.model;
 2 
 3 import javax.persistence.Embedded;
 4 import javax.persistence.Entity;
 5 import javax.persistence.GeneratedValue;
 6 import javax.persistence.Id;
 7 import javax.persistence.ManyToOne;
 8 import javax.persistence.OneToOne;
 9 import javax.persistence.Table;
10 
11 @Entity
12 @Table(name="student")
13 public class Student {
14     private int id;
15     private String name;
16     private int age;
17     
18     @Id
19     @GeneratedValue
20     public int getId() {
21         return id;
22     }
23     public void setId(int id) {
24         this.id = id;
25     }
26     public String getName() {
27         return name;
28     }
29     public void setName(String name) {
30         this.name = name;
31     }
32     public int getAge() {
33         return age;
34     }
35     public void setAge(int age) {
36         this.age = age;
37     }
38 }

三、运行测试

  

  出现一张中间表,两个id分别依赖teahcer的id和student的id;

四、修改student.java和teacher.java类(双向)

  双向:老师知道自己教了哪些学生,学生知道自己被哪几个老师教;

 1 package com.model;
 2 
 3 import java.util.HashSet;
 4 import java.util.Set;
 5 
 6 import javax.persistence.Embedded;
 7 import javax.persistence.Entity;
 8 import javax.persistence.GeneratedValue;
 9 import javax.persistence.Id;
10 import javax.persistence.ManyToMany;
11 import javax.persistence.ManyToOne;
12 import javax.persistence.OneToOne;
13 import javax.persistence.Table;
14 
15 @Entity
16 @Table(name="student")
17 public class Student {
18     private int id;
19     private String name;
20     private int age;
21     private Set<Teacher> teachers = new HashSet<Teacher>();
22     
23     @Id
24     @GeneratedValue
25     public int getId() {
26         return id;
27     }
28     public void setId(int id) {
29         this.id = id;
30     }
31     public String getName() {
32         return name;
33     }
34     public void setName(String name) {
35         this.name = name;
36     }
37     public int getAge() {
38         return age;
39     }
40     public void setAge(int age) {
41         this.age = age;
42     }
43     @ManyToMany(mappedBy="students")
44     public Set<Teacher> getTeachers() {
45         return teachers;
46     }
47     public void setTeachers(Set<Teacher> teachers) {
48         this.teachers = teachers;
49     }
50 }
 1 package com.model;
 2 
 3 import java.util.HashSet;
 4 import java.util.Set;
 5 
 6 import javax.persistence.Entity;
 7 import javax.persistence.GeneratedValue;
 8 import javax.persistence.Id;
 9 import javax.persistence.ManyToMany;
10 import javax.persistence.OneToMany;
11 import javax.persistence.Table;
12 
13 @Entity
14 @Table(name="teacher")
15 public class Teacher {
16     private int id;
17     private String teacherName;
18     private int teacherAge;
19     private String title;
20     private Set<Student> students = new HashSet<Student>();
21     
22     @Id
23     @GeneratedValue
24     public int getId() {
25         return id;
26     }
27     public void setId(int id) {
28         this.id = id;
29     }
30     public String getTeacherName() {
31         return teacherName;
32     }
33     public void setTeacherName(String teacherName) {
34         this.teacherName = teacherName;
35     }
36     public int getTeacherAge() {
37         return teacherAge;
38     }
39     public void setTeacherAge(int teacherAge) {
40         this.teacherAge = teacherAge;
41     }
42     public String getTitle() {
43         return title;
44     }
45     public void setTitle(String title) {
46         this.title = title;
47     }
48     @ManyToMany
49     public Set<Student> getStudents() {
50         return students;
51     }
52     public void setStudents(Set<Student> students) {
53         this.students = students;
54     }
55 }

五、测试运行

  

PS:虽然两次结果一样,但是双向关联中,可以通过teacher的students找到老师所教的学生,通过student的teachers找到学生的老师,单向只能从teacher的students找到老师所教的学生,而学生找不到老师

----------------------------------------------------------------------------------------------------

此文为个人学习记录,如有参考转载,请注明出处 黑白熊的博客 http://www.cnblogs.com/xiong233/

posted @ 2017-01-12 10:18  熊熊熊摸了一下你的头  阅读(122)  评论(0编辑  收藏  举报