hibernate多对多关系

上传人:子 文档编号:42571309 上传时间:2018-06-02 格式:DOC 页数:14 大小:18.57KB
返回 下载 相关 举报
hibernate多对多关系_第1页
第1页 / 共14页
hibernate多对多关系_第2页
第2页 / 共14页
hibernate多对多关系_第3页
第3页 / 共14页
hibernate多对多关系_第4页
第4页 / 共14页
hibernate多对多关系_第5页
第5页 / 共14页
点击查看更多>>
资源描述

《hibernate多对多关系》由会员分享,可在线阅读,更多相关《hibernate多对多关系(14页珍藏版)》请在金锄头文库上搜索。

1、hibernatehibernate 多对多关系多对多关系hibernate1.多对多关联的实现是通过中间表实现的。通过中间表,将这两个表之间的多对多关联关系转换为它们分别和中间表直接的一对多的关联关系。对于 Hibernate 来说,并不需要建立专门的中间对象来实现这种转换,但是中间表还是需要建立的。2.建立两个类2.1 user 类。用户表public class User implements Serializable private Set boards=new HashSet();/Set 接口 不可重复,无序性,不可比较相等。一个用户可以是多个板块的版主private Intege

2、r uid;/主键自增长private String uname;/姓名唯一private String upwd;/密码不为空public User(Set boards, Integer uid, String uname, String upwd) super();this.boards = boards;this.uid = uid;this.uname = uname;this.upwd = upwd;public User() super();public User(Integer uid, String uname, String upwd, Set boards) super(

3、);this.uid = uid;this.uname = uname;this.upwd = upwd;this.boards = boards;Overridepublic boolean equals(Object obj) if (this = obj)return true;if (obj = null)return false;if (getClass() != obj.getClass()return false;final User other = (User) obj;if (uid = null) if (other.uid != null)return false; el

4、se if (!uid.equals(other.uid)return false;return true;public Set getBoards() return boards;public Integer getUid() return uid;public String getUname() return uname;public String getUpwd() return upwd;Overridepublic int hashCode() final int prime = 31;int result = 1;result = prime * result + (uid = n

5、ull) ? 0 : uid.hashCode();return result;public void setBoards(Set boards) this.boards = boards;public void setUid(Integer uid) this.uid = uid;public void setUname(String uname) this.uname = uname;public void setUpwd(String upwd) this.upwd = upwd;-2.2.建立 board 类 板块表package com.entity;import java.io.S

6、erializable;import java.util.HashSet;import java.util.Set;SuppressWarnings(“serial“)public class Board implements Serializableprivate Integer bid;private String bname;private Set users=new HashSet();public Board(Integer bid, String bname, Set users) super();this.bid = bid;this.bname = bname;this.use

7、rs = users;public Board() super();/空构造的作用是什么?/hashcode 到底是什么东西?Overridepublic int hashCode() final int prime = 31;int result = 1;result = prime * result + (bid = null) ? 0 : bid.hashCode();return result;Overridepublic boolean equals(Object obj) if (this = obj)return true;if (obj = null)return false;

8、if (getClass() != obj.getClass()return false;final Board other = (Board) obj;if (bid = null) if (other.bid != null)return false; else if (!bid.equals(other.bid)return false;return true;public Integer getBid() /* 思考?安全访问器的写法?* *在 C#中构造器,怎么写?要是在 C 语言中呢?要是在javascript 中怎么写呢?* */ return bid;public void s

9、etBid(Integer bid) this.bid = bid;public String getBname() return bname;public void setBname(String bname) this.bname = bname;public Set getUsers() return users;public void setUsers(Set users) this.users = users;-3 对应映射文件this is a id type is native-/从下面建表语句中可以看出,u_b 联合主键表示的主键是primary key ( userId an

10、d boardId)/这样的 u-b 关系表是一个纽带,分别调用 usertable 和boardtable 表的主键作为外键/ create table Teacher.dbo.boardtable (boardId int identity not null,boardname varchar(255) not null unique,primary key (boardId)create table Teacher.dbo.usertable (userId Integer identity not null,username varchar(255) not null unique,p

11、rimary key (userId)create table u_b (boardId int not null,userId int not null,primary key (boardId, userId)alter table u_b add constraint FK1C318B689DC4F foreign key (userId) references Teacher.dbo.usertablealter table u_b add constraint FK1C3182581D6C9 foreign key (boardId) references Teacher.dbo.b

12、oardtable总结 :这是正确的配置 你可以读懂这些语句-如果在 user 类的配置文件这样配置 如果在 board 类的配置文件这样配置-执行创建文件后 会得到如下结果create table Teacher.dbo.boardtable (boardId int identity not null,boardname varchar(255) not null unique,primary key (boardId)create table Teacher.dbo.usertable (userId Integer identity not null,username varchar(

13、255) not null unique,primary key (userId)create table u_b (userId int not null,boardId int not null)alter table u_b add constraint FK1C318F0942E6E foreign key (userId) references Teacher.dbo.boardtablealter table u_b add constraint FK1C318B689DC4F foreign key (userId) references Teacher.dbo.usertabl

14、ealter table u_b add constraint FK1C318EB7784AA foreign key (boardId) references Teacher.dbo.usertablealter table u_b add constraint FK1C3182581D6C9 foreign key (boardId) references Teacher.dbo.boardtable/可以知道 我们的创建时不成功的 应为我们并未创建出联合主键表/以下的由于没有配置class=“com.entity.User“ 就是对应 user 类的映射表的列的示范 结果也是错误的/*

15、在 user 类的 Set 配置了* 这样相当于 board 类的映射的表使用了 boardId 作为 user 类映射的表的外键* 在 board 类的 Set 配置了* 这样相当于 user 类的映射的表使用了 usrId 作为 board 类映射的表的外键* 这样就乱了?怎么乱的? */ create table Teacher.dbo.boardtable (/ boardId int identity not null,/ boardname varchar(255) not null unique,/ primary key (boardId)/ )/ create table Teacher.dbo.usertable (/ userId Integer identity not null,/

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 科普知识

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号