Hibernate映射一对一关联关系

时间:2023-02-05 02:51:14

映射一对一关联

Hibernate提供了两种映射一对一关联关系的方式,分别是按照外键映射和按照主键映射。

下面是员工表和档案信息表(员工和档案表之间的关系是一对一的关系)

   档案表(dept)

 public class Dept {
private Integer deptid;
private String mystreet;
private Emp emp; public Dept() {
} public Dept(Integer deptid, String mystreet, Emp emp) {
this.deptid = deptid;
this.mystreet = mystreet;
this.emp = emp;
}
public Integer getDeptid() {
return deptid;
}
public void setDeptid(Integer deptid) {
this.deptid = deptid;
}
public String getMystreet() {
return mystreet;
}
public void setMystreet(String mystreet) {
this.mystreet = mystreet;
}
public Emp getEmp() {
return emp;
}
public void setEmp(Emp emp) {
this.emp = emp;
} }

员工表(emp)

 public class Emp {
private Integer empid;
private String myname;
private Dept dept; public Emp() {
} public Emp(Integer empid, String myname, Dept dept) {
this.empid = empid;
this.myname = myname;
this.dept = dept;
}
public Integer getEmpid() {
return empid;
}
public void setEmpid(Integer empid) {
this.empid = empid;
}
public String getMyname() {
return myname;
}
public void setMyname(String myname) {
this.myname = myname;
}
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
}

下面是第一种方案是按照主键映射

Emp的配置文件

 <?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.rm.onetoone_pk">
<class name="Emp" table="EMP2016">
<id name="empid" column="EMPID" type="int">
<generator class="sequence">
<param name="sequence">sq_id</param>
</generator>
</id>
<property name="myname" column="MYNAME" type="string"></property>
<one-to-one name="dept" class="Dept" property-ref="emp"></one-to-one>
<!-- property-ref为emp表明了Dept的emp属性建立了从Emp对象到Dept对象的关联 -->
</class> </hibernate-mapping>

Dept的配置文件

 <?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.rm.onetoone_pk">
<class name="Dept" table="DEPT2016">
<id name="deptid" column="DEPTID" type="int">
<generator class="sequence">
<param name="sequence">hb_ids</param>
</generator>
</id>
<property name="mystreet" column="MYSTREET" type="string"></property>
<many-to-one name="emp" class="Emp" cascade="all" unique="true" column="RESUSERID"></many-to-one>
<!-- cascade属性为all表示对本表的增 删 改 相关联的表都会自动更新 unique, 属性为true 表示唯一约束 column="RESUSERID" dept表的公共列-->
</class>
</hibernate-mapping>

这种方案的特点是:会在Dept表中增加一列 column="RESUSERID"这里就是与Emp相关联的列

第二种方案是按照外键映射

Emp的配置文件

 <?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.rm.onetoone_fk">
<class name="Emp" table="EMP2016fk">
<id name="empid" column="EMPID" type="int">
<generator class="foreign">
<param name="property">dept</param>
</generator>
</id>
<property name="myname" column="MYNAME" type="string"></property>
<one-to-one name="dept" class="Dept" constrained="true"></one-to-one>
<!-- constrained="true" 表示Emp表中的主键同时作为外键参照Emp表中的主键 -->
</class>
</hibernate-mapping>

Dept的配置文件

 <?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.rm.onetoone_fk">
<class name="Dept" table="DEPT2016fk">
<id name="deptid" column="DEPTID" type="int">
<generator class="sequence">
<param name="sequence">hb_ids</param>
</generator>
</id>
<property name="mystreet" column="MYSTREET" type="string"></property>
<one-to-one name="emp" class="Emp" cascade="all" ></one-to-one>
</class>
</hibernate-mapping>

这种方案的特点是员工表的主键和Dept表中的主键是相同的也就是Emp表和Dept表共享同一个OID