关于hibernate一对一配置
实体类关系
一对一
一对多
多对一
多对多
Hibernate提供了两种映射一对一关联关系的方式:按照外键映射和按照主键映射。下面以员工账号和员工档案表为例,介绍这两种映射方式,并使用这两种映射方式分别完成以下持久化操作
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配置如下:
<?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>
最后便是测试便好了