溫故知新:hibernate_12_表關聯_多對多雙向關聯(注解配置)

          和基于配置的多對多雙向關聯大體一致,修改實體類,添加注解,更改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 

          posted on 2015-01-21 11:22 都較瘦 閱讀(107) 評論(0)  編輯  收藏 所屬分類: ORMFramework

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統計

          公告

          博客定位:囿于目前的水平,博客定位在記錄自己的學習心得和隨手的練習

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 乃东县| 巴青县| 古蔺县| 肇州县| 兴安盟| 孝义市| 登封市| 宁陕县| 南皮县| 如东县| 许昌县| 璧山县| 绵竹市| 乐昌市| 濮阳市| 田东县| 巩留县| 比如县| 秦安县| 昭觉县| 灵璧县| 凤城市| 普定县| 青阳县| 任丘市| 专栏| 上饶县| 昌平区| 洪湖市| 灵石县| 鲁甸县| 双城市| 英山县| 张家川| 安化县| 京山县| 长乐市| 宁安市| 兴国县| 嘉祥县| 舞钢市|