Hibernate多對(duì)多關(guān)系映射
下邊講述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、建立表
- DROP TABLE user_course ;
- DROP TABLE user ;
- DROP TABLE course ;
- CREATE TABLE user (
- userid varchar(20) primary key ,
- name varchar(20) not null ,
- age int not null ,
- birthday date not null
- );
- CREATE TABLE course (
- id int primary key auto_increment ,
- title varchar(50) not null,
- description text not null,
- course_num int not null
- );
- CREATE TABLE user_course (
- userid varchar(20) ,
- cid int ,
- primary key (userid, cid ),
- foreign key (userid) references user (userid) on delete cascade ,
- foreign key (cid) references course (id) on delete cascade
- );
2、生成映射
選擇三個(gè)表一起生成映射,選擇主鍵生成方式的那一步需要注意:
然后每個(gè)表的主鍵生成方式,各自獨(dú)立設(shè)置,即點(diǎn)擊下一步再設(shè)置,對(duì)于中間表,不需要選擇主鍵生成方式(參考復(fù)合主鍵映射)。
3、查看pojo類
生成好的pojo包含了多對(duì)多關(guān)系,而且沒有生成中間關(guān)系表的映射。
- package org.liky.pojo;
- import java.util.Date;
- import java.util.HashSet;
- import java.util.Set;
- public class User implements java.io.Serializable {
- // Fields
- private String userid;
- private String name;
- private Integer age;
- private Date birthday;
- private Set courses = new HashSet(0);
- // Constructors
- public User() {
- }
- public User(String userid, String name, Integer age, Date birthday) {
- this.userid = userid;
- this.name = name;
- this.age = age;
- this.birthday = birthday;
- }
- public User(String userid, String name, Integer age, Date birthday,
- Set courses) {
- this.userid = userid;
- this.name = name;
- this.age = age;
- this.birthday = birthday;
- this.courses = courses;
- }
- // Property accessors
- public String getUserid() {
- return this.userid;
- }
- public void setUserid(String userid) {
- this.userid = userid;
- }
- public String getName() {
- return this.name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Integer getAge() {
- return this.age;
- }
- public void setAge(Integer age) {
- this.age = age;
- }
- public Date getBirthday() {
- return this.birthday;
- }
- public void setBirthday(Date birthday) {
- this.birthday = birthday;
- }
- public Set getCourses() {
- return this.courses;
- }
- public void setCourses(Set courses) {
- this.courses = courses;
- }
- }
- package org.liky.pojo;
- import java.util.HashSet;
- import java.util.Set;
- public class Course implements java.io.Serializable {
- // Fields
- private Integer id;
- private String title;
- private String description;
- private Integer courseNum;
- private Set users = new HashSet(0);
- // Constructors
- public Course() {
- }
- public Course(String title, String description, Integer courseNum) {
- this.title = title;
- this.description = description;
- this.courseNum = courseNum;
- }
- public Course(String title, String description, Integer courseNum, Set users) {
- this.title = title;
- this.description = description;
- this.courseNum = courseNum;
- this.users = users;
- }
- // Property accessors
- public Integer getId() {
- return this.id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getTitle() {
- return this.title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public String getDescription() {
- return this.description;
- }
- public void setDescription(String description) {
- this.description = description;
- }
- public Integer getCourseNum() {
- return this.courseNum;
- }
- public void setCourseNum(Integer courseNum) {
- this.courseNum = courseNum;
- }
- public Set getUsers() {
- return this.users;
- }
- public void setUsers(Set users) {
- this.users = users;
- }
- }
【編輯推薦】