One-to-one relationship
Hibernate provides two ways to map one-to-one relationships: mapping by foreign key and mapping by primary key. The following takes employee account and employee file table as examples to introduce these two mapping methods, and use these two mapping methods to complete the following persistence operations:
(1) Assign an account to the employee while saving the employee file.
(2) Load the account information while loading the employee file.
Map by foreign key:
relation chart:
①Create entity classes: Resume, Users and encapsulate properties
public class Resume { //file id private Integer resid; //file name private String resname; //file number private String rescardno; // employee object private Users users; }
public class Users { // employee id private Integer userid; //name of worker private String username; // employee password private String userpass; //file object private Resume resume; }
② Configure the small configuration Users.hbm.xml
Note: property-ref="users" indicates that when looking for the resume attribute of Users, the users attribute of Resume will be used to match the primary key attribute of Users
<?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"/> <!--Lord's side--> <one-to-one name="users2" cascade="all" class="Users2" /> </class> </hibernate-mapping>
Reprinted from: http://www.cnblogs.com/hr1997/p/5845303.html