下边讲述Hibernate多对多关系映射。

桂林ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联建站的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18980820575(备注:SSL证书合作)期待与您的合作!
多对多关系的表的结构为:
两个实体表,还包含一个关系表,关系表为复合主键,如果要使用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多对多关系映射
                
                文章来源:http://www.csdahua.cn/qtweb/news30/509930.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网