Hibernate多对多关系映射
创始人
2024-04-03 21:01:30
0

下边讲述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、生成映射

选择三个表一起生成映射,选择主键生成方式的那一步需要注意:

然后每个表的主键生成方式,各自独立设置,即点击下一步再设置,对于中间表,不需要选择主键生成方式(参考复合主键映射)。

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的性能优化

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...