日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Hibernate多對多關系映射

下邊講述Hibernate多對多關系映射。

桂林ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)建站的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!

多對多關系的表的結(jié)構為:

兩個實體表,還包含一個關系表,關系表為復合主鍵,如果要使用Hibernate多對多關系映射,則關系表必須只包含兩個字段,如果生成了Hibernate多對多關系映射,則中間關系表不會生成實體(即沒有對應的pojo類,更沒有其映射文件)。

1、建立表

 
 
 
  1. DROP TABLE user_course ;  
  2. DROP TABLE user ;  
  3. DROP TABLE course ;  
  4. CREATE TABLE user (  
  5.     userid            varchar(20)              primary key ,  
  6.     name              varchar(20)              not null ,  
  7.     age               int                  not null ,  
  8.     birthday          date                 not null 
  9. );  
  10. CREATE TABLE course (  
  11.     id                int                  primary key auto_increment ,  
  12.     title             varchar(50)              not null,  
  13.     description          text                 not null,  
  14.    course_num        int                  not null 
  15. );  
  16. CREATE TABLE user_course (  
  17.     userid            varchar(20)              ,  
  18.     cid               int                  ,  
  19.     primary key (userid, cid ),  
  20.     foreign key (userid) references user (userid) on delete cascade ,  
  21.     foreign key (cid) references course (id) on delete cascade  
  22. ); 

2、生成映射

選擇三個表一起生成映射,選擇主鍵生成方式的那一步需要注意:

然后每個表的主鍵生成方式,各自獨立設置,即點擊下一步再設置,對于中間表,不需要選擇主鍵生成方式(參考復合主鍵映射)。

3、查看pojo類
 
生成好的pojo包含了多對多關系,而且沒有生成中間關系表的映射。

 
 
 
  1. package org.liky.pojo;  
  2. import java.util.Date;  
  3. import java.util.HashSet;  
  4. import java.util.Set;  
  5. public class User implements java.io.Serializable {  
  6.     // Fields  
  7.     private String userid;  
  8.     private String name;  
  9.     private Integer age;  
  10.     private Date birthday;  
  11.     private Set courses = new HashSet(0);  
  12.     // Constructors  
  13.     public User() {  
  14.     }  
  15.     public User(String userid, String name, Integer age, Date birthday) {  
  16.        this.userid = userid;  
  17.        this.name = name;  
  18.        this.age = age;  
  19.        this.birthday = birthday;  
  20.     }  
  21.     public User(String userid, String name, Integer age, Date birthday,  
  22.            Set courses) {  
  23.        this.userid = userid;  
  24.        this.name = name;  
  25.        this.age = age;  
  26.        this.birthday = birthday;  
  27.        this.courses = courses;  
  28.     }  
  29.     // Property accessors  
  30.     public String getUserid() {  
  31.        return this.userid;  
  32.     }  
  33.     public void setUserid(String userid) {  
  34.        this.userid = userid;  
  35.     }  
  36.     public String getName() {  
  37.        return this.name;  
  38.     }  
  39.     public void setName(String name) {  
  40.        this.name = name;  
  41.     }  
  42.     public Integer getAge() {  
  43.        return this.age;  
  44.     }  
  45.     public void setAge(Integer age) {  
  46.        this.age = age;  
  47.     }  
  48.     public Date getBirthday() {  
  49.        return this.birthday;  
  50.     }  
  51.     public void setBirthday(Date birthday) {  
  52.        this.birthday = birthday;  
  53.     }  
  54.     public Set getCourses() {  
  55.        return this.courses;  
  56.     }  
  57.     public void setCourses(Set courses) {  
  58.        this.courses = courses;  
  59.     }  
  60. }  
  61. package org.liky.pojo;  
  62. import java.util.HashSet;  
  63. import java.util.Set;  
  64. public class Course implements java.io.Serializable {  
  65.     // Fields  
  66.     private Integer id;  
  67.     private String title;  
  68.     private String description;  
  69.     private Integer courseNum;  
  70.     private Set users = new HashSet(0);  
  71.     // Constructors  
  72.     public Course() {  
  73.     }  
  74.     public Course(String title, String description, Integer courseNum) {  
  75.        this.title = title;  
  76.        this.description = description;  
  77.        this.courseNum = courseNum;  
  78.     }  
  79.     public Course(String title, String description, Integer courseNum, Set users) {  
  80.        this.title = title;  
  81.        this.description = description;  
  82.        this.courseNum = courseNum;  
  83.        this.users = users;  
  84.     }  
  85.     // Property accessors  
  86.     public Integer getId() {  
  87.        return this.id;  
  88.     }  
  89.     public void setId(Integer id) {  
  90.        this.id = id;  
  91.     }  
  92.     public String getTitle() {  
  93.        return this.title;  
  94.     }  
  95.     public void setTitle(String title) {  
  96.        this.title = title;  
  97.     }  
  98.     public String getDescription() {  
  99.        return this.description;  
  100.     }  
  101.     public void setDescription(String description) {  
  102.        this.description = description;  
  103.     }  
  104.     public Integer getCourseNum() {  
  105.        return this.courseNum;  
  106.     }  
  107.     public void setCourseNum(Integer courseNum) {  
  108.        this.courseNum = courseNum;  
  109.     }  
  110.     public Set getUsers() {  
  111.        return this.users;  
  112.     }  
  113.     public void setUsers(Set users) {  
  114.        this.users = users;  
  115.     }  

【編輯推薦】

  1. 強人Hibernate文檔筆記(上)
  2. 強人Hibernate文檔筆記(中)
  3. 強人Hibernate文檔筆記(下)
  4. Hibernate一對多關系的處理
  5. Hibernate的性能優(yōu)化

當前標題:Hibernate多對多關系映射
標題路徑:http://m.5511xx.com/article/dpgpgod.html