日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

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

新聞中心

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

下邊講述Hibernate多對(duì)多關(guān)系映射。

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

多對(duì)多關(guān)系的表的結(jié)構(gòu)為:

兩個(gè)實(shí)體表,還包含一個(gè)關(guān)系表,關(guān)系表為復(fù)合主鍵,如果要使用Hibernate多對(duì)多關(guān)系映射,則關(guān)系表必須只包含兩個(gè)字段,如果生成了Hibernate多對(duì)多關(guān)系映射,則中間關(guān)系表不會(huì)生成實(shí)體(即沒(méi)有對(duì)應(yīng)的pojo類(lèi),更沒(méi)有其映射文件)。

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、生成映射

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

然后每個(gè)表的主鍵生成方式,各自獨(dú)立設(shè)置,即點(diǎn)擊下一步再設(shè)置,對(duì)于中間表,不需要選擇主鍵生成方式(參考復(fù)合主鍵映射)。

3、查看pojo類(lèi)
 
生成好的pojo包含了多對(duì)多關(guān)系,而且沒(méi)有生成中間關(guān)系表的映射。

 
 
 
  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. 強(qiáng)人Hibernate文檔筆記(上)
  2. 強(qiáng)人Hibernate文檔筆記(中)
  3. 強(qiáng)人Hibernate文檔筆記(下)
  4. Hibernate一對(duì)多關(guān)系的處理
  5. Hibernate的性能優(yōu)化

文章標(biāo)題:Hibernate多對(duì)多關(guān)系映射
分享網(wǎng)址:http://www.dlmjj.cn/article/dpgpgod.html