一、一对一关联
Hibernate提供了两种映射一对一关联关系的方式:按照外键映射和按照主键映射。下面以员工账号和员工档案表为例,介绍这两种映射方式,并使用这两种映射方式分别完成以下持久化操作:
(1)保存员工档案的同时分配给员工一个账号。
(2)加载员工档案的同时加载账号信息。
按照外键映射:
关系图:
①创建实体类:Resume、Users 并封装属性
public class Resume {
//档案id
private Integer resid;
//档案名称
private String resname;
//档案编号
private String rescardno;
//员工对象
private Users users;
}
public class Users {
//员工id
private Integer userid;
//员工姓名
private String username;
//员工密码
private String userpass;
//档案对象
private Resume resume;
}
② 配置小配置 Users.hbm.xml
注:property-ref="users" 表明Resume的users属性建立了从Users对象到Resume对象的关联
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.happy.entity">
<class name="Users" table="USERS1">
<id name="userid" column="USERID" >
<generator class="native"></generator>
</id>
<property name="username" column="USERNAME" type="string"></property>
<property name="userpass" column="USERPASS" type="string"></property>
<!-- 配置一对一外键关系的关联 -->
<one-to-one name="resume" class="Resume" property-ref="users"></one-to-one>
</class>
</hibernate-mapping>
Resume.hbm.xml
注:因为Resume为外键表 植入<many-to-one>元素 并设置属性unique=“true” 确保用户档案的列只能是唯一的, 一个档案对应一个用户编号
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.happy.entity">
<class name="Resume" table="RESUME1">
<id column="RESID" name="resid">
<generator class="native"></generator>
</id>
<property column="RESNAME" name="resname" type="string"/>
<property column="RESCARDNO" name="rescardno" type="string"/>
<!--主的一方 --> <!-- 在用户档案指定的列只能是唯一的 一个档案只能对应一个用户编号-->
<many-to-one name="users" cascade="all" class="Users" column="RESCARDID" unique="true"/>
</class>
</hibernate-mapping>
③ 大配置进行关联小配置
<!-- 关联小配置 --> <mapping resource="cn/happy/entity/Users.hbm.xml" />
<mapping resource="cn/happy/entity/Resume.hbm.xml" />
测试类:
/*
* 添加
*/ @Test
public void addTest(){
//创建用户对象
Users u=new Users();
u.setUsername("张三1");
u.setUserpass("003");
//创建档案对象
Resume r=new Resume();
r.setResname("大学文凭1");
r.setRescardno("003");
//关联
u.setResume(r);
r.setUsers(u);
//保存档案,员工自动save
session.save(r);
System.out.println("save ok!!!"); }
输出结果:
按照主键映射:
关系图如下:
实体类同上
其次就是小配置的更改。Resume为主键表,Users的Userid既是主键又是外键 因此植入元素generator的类型为foreign主键
<one-to-one>植入属性constrained 用来约束 在底层数据表中植入外键
Users.hbm.xml配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.happy.pk">
<class name="Users2" table="USERS2">
<id name="userid" column="USERID" >
<generator class="foreign">
<param name="property">resume2</param>
</generator>
</id>
<property name="username" column="USERNAME" type="string"></property>
<property name="userpass" column="USERPASS" type="string"></property>
<!-- constrained:用来约束 在底层USERS2数据表中,植入外键-->
<one-to-one name="resume2" class="Resume2" constrained="true"></one-to-one>
</class>
</hibernate-mapping>
Resume.hbm.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.happy.pk">
<class name="Resume2" table="RESUME2">
<id column="RESID" name="resid">
<generator class="sequence">
<param name="sequence">SEQ_NUM</param>
</generator>
</id>
<property column="RESNAME" name="resname" type="string"/>
<property column="RESCARDNO" name="rescardno" type="string"/>
<!--主的一方 -->
<one-to-one name="users2" cascade="all" class="Users2" />
</class>
</hibernate-mapping>
输出结果如下: