自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Hibernate多對(duì)多關(guān)系映射

開發(fā) 后端
兩個(gè)實(shí)體表,還包含一個(gè)關(guān)系表,關(guān)系表為復(fù)合主鍵,如果要使用Hibernate多對(duì)多關(guān)系映射,則關(guān)系表必須只包含兩個(gè)字段,如果生成了Hibernate多對(duì)多關(guān)系映射,則中間關(guān)系表不會(huì)生成實(shí)體。

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

多對(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í)體(即沒有對(duì)應(yīng)的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、生成映射

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

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

3、查看pojo類
 
生成好的pojo包含了多對(duì)多關(guān)系,而且沒有生成中間關(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)化
責(zé)任編輯:仲衡 來(lái)源: 紫色飛鳥的博客
相關(guān)推薦

2012-02-08 13:34:08

HibernateJava

2009-06-04 10:34:19

Hibernate一對(duì)一對(duì)多關(guān)系配置

2009-09-23 13:26:10

Hibernate對(duì)象

2009-09-25 12:59:52

Hibernate映射

2009-06-04 16:14:22

Hibernate一對(duì)Hibernate一對(duì)Hibernate多對(duì)

2023-06-12 08:09:01

FlaskSQLAlchemy

2009-06-02 14:46:26

Hibernate關(guān)系映射教程

2012-02-08 12:17:38

HibernateJava

2009-06-16 14:36:54

Hibernate繼承

2009-09-22 09:55:58

Hibernate實(shí)例

2009-07-21 17:39:50

iBATIS的多對(duì)多映

2012-05-30 15:03:43

ibmdw

2012-03-21 11:43:41

JavaHibernate

2010-04-15 09:09:02

Hibernate

2009-07-21 17:31:39

iBATIS一對(duì)多映射

2012-02-02 16:13:29

HibernateJava

2009-09-25 14:12:16

Hibernate繼承

2009-09-23 17:34:18

Hibernate映射

2009-09-25 10:00:47

Hibernate映射

2012-02-03 10:07:04

HibernateJava
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)