Hibernate映射一对一关联关系
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate映射一对一关联关系相关的知识,希望对你有一定的参考价值。
映射一对一关联
Hibernate提供了两种映射一对一关联关系的方式,分别是按照外键映射和按照主键映射。
下面是员工表和档案信息表(员工和档案表之间的关系是一对一的关系)
档案表(dept)
1 public class Dept { 2 private Integer deptid; 3 private String mystreet; 4 private Emp emp; 5 6 public Dept() { 7 } 8 9 public Dept(Integer deptid, String mystreet, Emp emp) { 10 this.deptid = deptid; 11 this.mystreet = mystreet; 12 this.emp = emp; 13 } 14 public Integer getDeptid() { 15 return deptid; 16 } 17 public void setDeptid(Integer deptid) { 18 this.deptid = deptid; 19 } 20 public String getMystreet() { 21 return mystreet; 22 } 23 public void setMystreet(String mystreet) { 24 this.mystreet = mystreet; 25 } 26 public Emp getEmp() { 27 return emp; 28 } 29 public void setEmp(Emp emp) { 30 this.emp = emp; 31 } 32 33 }
员工表(emp)
1 public class Emp { 2 private Integer empid; 3 private String myname; 4 private Dept dept; 5 6 public Emp() { 7 } 8 9 public Emp(Integer empid, String myname, Dept dept) { 10 this.empid = empid; 11 this.myname = myname; 12 this.dept = dept; 13 } 14 public Integer getEmpid() { 15 return empid; 16 } 17 public void setEmpid(Integer empid) { 18 this.empid = empid; 19 } 20 public String getMyname() { 21 return myname; 22 } 23 public void setMyname(String myname) { 24 this.myname = myname; 25 } 26 public Dept getDept() { 27 return dept; 28 } 29 public void setDept(Dept dept) { 30 this.dept = dept; 31 } 32 }
下面是第一种方案是按照主键映射
Emp的配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping package="cn.rm.onetoone_pk"> 6 <class name="Emp" table="EMP2016"> 7 <id name="empid" column="EMPID" type="int"> 8 <generator class="sequence"> 9 <param name="sequence">sq_id</param> 10 </generator> 11 </id> 12 <property name="myname" column="MYNAME" type="string"></property> 13 <one-to-one name="dept" class="Dept" property-ref="emp"></one-to-one> 14 <!-- property-ref为emp表明了Dept的emp属性建立了从Emp对象到Dept对象的关联 --> 15 </class> 16 17 </hibernate-mapping>
Dept的配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping package="cn.rm.onetoone_pk"> 6 <class name="Dept" table="DEPT2016"> 7 <id name="deptid" column="DEPTID" type="int"> 8 <generator class="sequence"> 9 <param name="sequence">hb_ids</param> 10 </generator> 11 </id> 12 <property name="mystreet" column="MYSTREET" type="string"></property> 13 <many-to-one name="emp" class="Emp" cascade="all" unique="true" column="RESUSERID"></many-to-one> 14 <!-- cascade属性为all表示对本表的增 删 改 相关联的表都会自动更新 unique, 属性为true 表示唯一约束 column="RESUSERID" dept表的公共列--> 15 </class> 16 </hibernate-mapping>
这种方案的特点是:会在Dept表中增加一列 column="RESUSERID"这里就是与Emp相关联的列
第二种方案是按照外键映射
Emp的配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping package="cn.rm.onetoone_fk"> 6 <class name="Emp" table="EMP2016fk"> 7 <id name="empid" column="EMPID" type="int"> 8 <generator class="foreign"> 9 <param name="property">dept</param> 10 </generator> 11 </id> 12 <property name="myname" column="MYNAME" type="string"></property> 13 <one-to-one name="dept" class="Dept" constrained="true"></one-to-one> 14 <!-- constrained="true" 表示Emp表中的主键同时作为外键参照Emp表中的主键 --> 15 </class> 16 </hibernate-mapping>
Dept的配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping package="cn.rm.onetoone_fk"> 6 <class name="Dept" table="DEPT2016fk"> 7 <id name="deptid" column="DEPTID" type="int"> 8 <generator class="sequence"> 9 <param name="sequence">hb_ids</param> 10 </generator> 11 </id> 12 <property name="mystreet" column="MYSTREET" type="string"></property> 13 <one-to-one name="emp" class="Emp" cascade="all" ></one-to-one> 14 </class> 15 </hibernate-mapping>
这种方案的特点是员工表的主键和Dept表中的主键是相同的也就是Emp表和Dept表共享同一个OID
以上是关于Hibernate映射一对一关联关系的主要内容,如果未能解决你的问题,请参考以下文章
Hibernate—— 联合主键 一对一关联关系映射(xml和注解) 和 领域驱动设计