新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Hibernate多對多關系映射
下邊講述Hibernate多對多關系映射。

桂林ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)建站的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!
多對多關系的表的結(jié)構為:
兩個實體表,還包含一個關系表,關系表為復合主鍵,如果要使用Hibernate多對多關系映射,則關系表必須只包含兩個字段,如果生成了Hibernate多對多關系映射,則中間關系表不會生成實體(即沒有對應的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、生成映射
選擇三個表一起生成映射,選擇主鍵生成方式的那一步需要注意:
然后每個表的主鍵生成方式,各自獨立設置,即點擊下一步再設置,對于中間表,不需要選擇主鍵生成方式(參考復合主鍵映射)。
3、查看pojo類
生成好的pojo包含了多對多關系,而且沒有生成中間關系表的映射。
- 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;
- }
- }
【編輯推薦】
- 強人Hibernate文檔筆記(上)
- 強人Hibernate文檔筆記(中)
- 強人Hibernate文檔筆記(下)
- Hibernate一對多關系的處理
- Hibernate的性能優(yōu)化
當前標題:Hibernate多對多關系映射
標題路徑:http://m.5511xx.com/article/dpgpgod.html


咨詢
建站咨詢
