(由于映射文件在orm映射文件配置中已经讲过,这里就不在赘述)
实体之间的关系分为:一对一,一对多,多对多
1、一对一关系映射
一对一有两种方式:
主键关联
:从表的主键,同时又是主表的外键。从表没有单独的外键列。
外键关联
:从表中存在外键列,关联主表的主键列。但外键列是不重复。
主表一方有从表一方的外键,是多对一关联映射的特例。
一对一关联测试运行
<span style="font-size:18px;">public void testOneToOneRelation(){
Citizen citizen = new Citizen();
citizen.setName("Tom");
citizen.setAge(20);
Card card = new Card();
card.setCode("65280119820612051");
card.setOrganization("JingLiu area");
//相互设置关联
citizen.setCard(card);
card.setCitizen(citizen);
//保存(由于citizen的配置中设置了cascade=“all”属性,因此,可以进行级联保存)
Transaction tx = session.beginTransaction();
session.save(citizen); //只需从主控方保存即可
tx.commit();</span>
cascade=“all”级联操作
(all,none,delete,save-update)当当前对象执行某操作的情况下,其关联的对象也执行cascade设置的同样操作。
cascade和inverse的区别:
cascade表示级联操作,当主表记录做操作时,从表记录做响应的操作,维护的是记录。
inverse表示控制反转。当该属性设置为真时,表示由关联对象维护外键的关系,当前对象不做外键维护,维护的是外键。一般来说,inverse设置在一方,外键由从表维护。
2、一对多关系映射
一方实体bean:
<span style="font-size:18px;">public class UserBean {
private int id;
private String name;
private string phone;
//聚合多方的实体集合
private Set<RoomBean> room= new HashSet<room>();
}</span>
多方实体bean:
<span style="font-size:18px;">public class RoomSet {
private int id;
private String address;
private String roomNumber;
private int money;
}</span>
一对多时删除主表要先将外键置空
/**
将外键置空
*/
for (Object room : bean.getroomSet()) {
RoomBean rb = (RoomBean) room;
rb.setUser(null);
}
一对多主表的添加以及删除
<span style="font-size:18px;">public void add(UserBean bean, Set<RoomBean> roomSet) {
Session session = this.getSession();//得到会话工厂
session.beginTransaction(); //开启事务
try {
//遍历从表对象,在从表对象中设置主表对象
for (RoomBean room : roomSet) {
room.setUser(bean);
}
//在主表对象中设置从表对象
bean.setroomSet(roomSet);
session.save(bean);
session.getTransaction().commit();
} catch (Exception e) {
session.getTransaction().rollback();
} finally {
session.close();
}
}
@Override
public void del(int id) {
Session session = this.getSession();
session.beginTransaction();
try {
//得到主表对象
UserBean bean = (UserBean) session.get(UserBean.class, id);
//将外键置空
for (Object room : bean.getroomSet()) {
RoomBean rb = (RoomBean) room;
rb.setUser(null);
}
session.delete(bean);
session.getTransaction().commit();
} catch (Exception e) {
session.getTransaction().rollback();
} finally {
session.close();
}
}</span><span style="font-size: 14px;">
</span>
3、多对多关系映射
* 角色实体类
* @author Administrator
*
*/
public class RoleBean {
private int id;
private String roleName;
private Set gradeSet;
public RoleBean() {
super();
}
public RoleBean(String roleName, Set gradeSet) {
super();
this.roleName = roleName;
this.gradeSet = gradeSet;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public Set getGradeSet() {
return gradeSet;
}
public void setGradeSet(Set gradeSet) {
this.gradeSet = gradeSet;
}
@Override
public String toString() {
return "RoleBean [id=" + id + ", roleName=" + roleName + "]";
}
}</span>
<span style="font-size:18px;">/**
* 权限实体类
* @author Administrator
*
*/
public class GradeBean {
private int id;
private String gradeName;
private Set roleSet;
public GradeBean() {
super();
}
public GradeBean(String gradeName, Set roleSet) {
super();
this.gradeName = gradeName;
this.roleSet = roleSet;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getGradeName() {
return gradeName;
}
public void setGradeName(String gradeName) {
this.gradeName = gradeName;
}
public Set getRoleSet() {
return roleSet;
}
public void setRoleSet(Set roleSet) {
this.roleSet = roleSet;
}
@Override
public String toString() {
return "GradeBean [id=" + id + ", gradeName=" + gradeName + "]";
}
}
多对多主表的添加:
多对多时在数据库需要建立中间表来关联
<span style="font-size:18px;">public void add(RoleBean bean, int[] gradeArray) {
Session session = this.getSession();
session.beginTransaction();
try {
//循环添加从表对象
for(int gradeId : gradeArray){
GradeBean grade = (GradeBean)session.get(GradeBean.class, gradeId);
bean.getGradeSet().add(grade);
}
session.save(bean);
session.getTransaction().commit();
} catch (Exception e) {
session.getTransaction().rollback();
}finally {
session.close();
}
}
//修改
public void update(int id, int[] gradeArray) {
Session session = this.getSession();
session.beginTransaction();
try {
RoleBean bean = (RoleBean)session.get(RoleBean.class,id);
//修改之前先清空集合
bean.getGradeSet().clear();
//循环添加从表对象
for(int gradeId : gradeArray){
GradeBean grade = (GradeBean)session.get(GradeBean.class, gradeId);
bean.getGradeSet().add(grade);
}
session.getTransaction().commit();
} catch (Exception e) {
session.getTransaction().rollback();
}finally{
session.close();
}
}
</span>