Hibernate多對多關係映射(建表)

來源:互聯網
上載者:User

標籤:

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

多對多關係的表的結構為:

兩個實體表,還包含一個關係表,關係表為複合主鍵,如果要使用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、產生映射

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

然後每個表的主鍵產生方式,各自獨立設定,即點擊下一步再設定,對於中間表,不需要選擇主鍵產生方式(參考複合主鍵映射)。

Hibernate多對多關係映射(建表)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.