和基于配置的多对多双向关联大体一致,修改实体类,添加注解,更改hibernate.cfg.xml,引入实体类只列出实体类的变动
Course
1 package domain;
2
3 import java.util.Set;
4
5 import javax.persistence.Entity;
6 import javax.persistence.GeneratedValue;
7 import javax.persistence.Id;
8 import javax.persistence.OneToMany;
9 import javax.persistence.Table;
10
11 @Entity
12 @Table(name="t_course")
13 public class Course {
14
15 private int id;
16 private String name;
17
18 private Set<StudentCourse> studentCourses;
19
20 public Course() {
21 super();
22 }
23
24 public Course(int id, String name, Set<StudentCourse> studentCourses) {
25 super();
26 this.id = id;
27 this.name = name;
28 this.studentCourses = studentCourses;
29 }
30
31 @Id
32 @GeneratedValue
33 public int getId() {
34 return id;
35 }
36
37 public void setId(int id) {
38 this.id = id;
39 }
40
41 public String getName() {
42 return name;
43 }
44
45 public void setName(String name) {
46 this.name = name;
47 }
48
49 @OneToMany(mappedBy="course")
50 public Set<StudentCourse> getStudentCourses() {
51 return studentCourses;
52 }
53
54 public void setStudentCourses(Set<StudentCourse> studentCourses) {
55 this.studentCourses = studentCourses;
56 }
57
58 }
59
Student
1 package domain;
2
3 import java.util.Set;
4
5 import javax.persistence.Entity;
6 import javax.persistence.GeneratedValue;
7 import javax.persistence.Id;
8 import javax.persistence.OneToMany;
9 import javax.persistence.Table;
10
11 @Entity
12 @Table(name="t_student")
13 public class Student {
14
15 private int id;
16 private String name;
17
18 private Set<StudentCourse> studentCourses;
19
20 public Student() {
21 super();
22 }
23
24 public Student(int id, String name, Set<StudentCourse> studentCourses) {
25 super();
26 this.id = id;
27 this.name = name;
28 this.studentCourses = studentCourses;
29 }
30
31 @Id
32 @GeneratedValue
33 public int getId() {
34 return id;
35 }
36
37 public void setId(int id) {
38 this.id = id;
39 }
40
41 public String getName() {
42 return name;
43 }
44
45 public void setName(String name) {
46 this.name = name;
47 }
48
49 @OneToMany(mappedBy="student")
50 public Set<StudentCourse> getStudentCourses() {
51 return studentCourses;
52 }
53
54 public void setStudentCourses(Set<StudentCourse> studentCourses) {
55 this.studentCourses = studentCourses;
56 }
57
58 }
59
StudentCourse
1 package domain;
2
3 import javax.persistence.Entity;
4 import javax.persistence.GeneratedValue;
5 import javax.persistence.Id;
6 import javax.persistence.JoinColumn;
7 import javax.persistence.ManyToOne;
8 import javax.persistence.Table;
9
10 @Entity
11 @Table(name="t_student_course")
12 public class StudentCourse {
13
14 private int id;
15 private int point;
16
17 private Student student;
18 private Course course;
19 public StudentCourse() {
20 super();
21 }
22 public StudentCourse(int id, int point, Student student, Course course) {
23 super();
24 this.id = id;
25 this.point = point;
26 this.student = student;
27 this.course = course;
28 }
29
30 @Id
31 @GeneratedValue
32 public int getId() {
33 return id;
34 }
35 public void setId(int id) {
36 this.id = id;
37 }
38 public int getPoint() {
39 return point;
40 }
41 public void setPoint(int point) {
42 this.point = point;
43 }
44
45 @ManyToOne
46 @JoinColumn(name="sid")
47 public Student getStudent() {
48 return student;
49 }
50 public void setStudent(Student student) {
51 this.student = student;
52 }
53 @ManyToOne
54 @JoinColumn(name="cid")
55 public Course getCourse() {
56 return course;
57 }
58 public void setCourse(Course course) {
59 this.course = course;
60 }
61
62 }
63