首页 > 代码库 > hibernate.一对一关联
hibernate.一对一关联
实体类关系
一对一
一对多
多对一
多对多
Hibernate提供了两种映射一对一关联关系的方式:按照外键映射和按照主键映射。下面以员工账号和员工档案表为例,介绍这两种映射方式,并使用这两种映射方式分别完成以下持久化操作:
(1)保存员工档案的同时分配给员工一个账号。
(2)加载员工档案的同时加载账号信息。
1.按照外键映射
步骤一:创建实体类Users1和Resume1
Users1创建如下:
public class Users1 {
private Integer userid;
private String username;
private String userpass;
private Resume1 resume1;
}
Resume1创建如下:
public class Resume1 {
private Integer resid;
private String resname;
private String rescardno;
private Users1 users1;
}
步骤二:配置文件Users1.hbm.xml和Resume1.hbm.xml
Users1.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.onetoone_fk">
<class name="Users1" 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="resume1" class="Resume1" property-ref="users1"></one-to-one>
</class>
</hibernate-mapping>
Resume1.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.onetoone_fk">
<class name="Resume1" table="RESUME1">
<id name="resid" column="RESID" >
<generator class="native"></generator>
</id>
<property name="resname" column="RESNAME" type="string"></property>
<property name="rescardno" column="RESCARDNO" type="string"></property>
<many-to-one name="users1" class="Users1" cascade="all" column="RESUSERID" unique="true"></many-to-one>
</class>
</hibernate-mapping>
步骤三:测试方法书写
@Test
public void testAdd(){
Session session = HibernateUtil.getSession();
Transaction tx=session.beginTransaction();
//创建一个用户对象
Users1 u1=new Users1("happy","1");
//创建一个档案对象
Resume1 r1=new Resume1("小学档案","happy01");
u1.setResume1(r1);
r1.setUsers1(u1);
//保存r1自动保存u1
1.按照主键映射
步骤一:创建实体类Users2和Resume2
Users2的创建如下:
public class Users2 {
private Integer userid;
private String username;
private String userpass;
private Resume2 resume2;
}
Resume2的创建如下:
public class Resume2 {
private Integer resid;
private String resname;
private String rescardno;
private Users2 users2;
}
步骤二:配置文件Users1.hbm.xml和Resume1.hbm.xml
Users1.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.onetoone_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>
<one-to-one name="resume2" class="Resume2" constrained="true"></one-to-one>
</class>
</hibernate-mapping>
Resume2.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.onetoone_pk">
<class name="Resume2" table="RESUME2">
<id column="RESID" name="resid">
<generator class="native"/>
</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>
session.save(r1);
tx.commit();
System.out.println("ok===");
}
02.组件映射
建立关系数据模型的一个重要原则是在不会导致数据冗余的前提下,尽可能减少数据库表的数目及表之间的外键参照关系。以员工信息为例,员工信息中有员工的家庭地址信息,如果把地址信息单独放在一张表中,然后建立员工信息表和地址信息表之间的外键参照关系,当每次查询员工信息时,都需建立者两个表的连接。建立表的连接是很耗时的操作,为了提高数据库运行性能,可以把这两张表的信息整合在一张员工信息表EMPINFO中。
步骤一:创建EmpHomeAddress和EmpInfo
public class EmpHomeAddress {
private String ehomestreet;
private String ehomecity;
private String ehomeprovince;
private String ehomezipcode;
private EmpInfo empinfo;
}
EmpInfo创建如下:
public class EmpInfo {
private Integer eid;
private String ename;
private EmpHomeAddress ehome;
}
步骤二:创建配置文件EmpInfo.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.component">
<class name="EmpInfo" table="EMPINFO">
<id name="eid" column="EID">
<generator class="native"></generator>
</id>
<property name="ename" column="ENAME" type="string"></property>
<component name="ehome" class="EmpHomeAddress">
<parent name="empinfo"/>
<property name="ehomestreet" column="EHOMESTREET" type="string"></property>
<property name="ehomecity" column="EHOMECITY" type="string"></property>
<property name="ehomeprovince" column="EHOMEPROVINCE" type="string"></property>
<property name="ehomezipcode" column="EHOMEZIPCODE" type="string"></property>
</component>
</class>
</hibernate-mapping>
步骤三:书写测试类
@Test
public void testAdd(){
Session session = HibernateUtil.getSession();
Transaction tx=session.beginTransaction();
//创建一个员工对象
EmpInfo emp=new EmpInfo();
emp.setEname("张靓颖");
//创建一个员工地址对象
EmpHomeAddress address=new EmpHomeAddress();
address.setEhomecity("北京");
address.setEhomeprovince("北京");
address.setEhomestreet("五道口");
address.setEhomezipcode("100000");
address.setEmpinfo(emp);
emp.setEhome(address);
session.save(emp);
tx.commit();
System.out.println("ok===");
}
hibernate.一对一关联