双向一对一关联关系映射中,关系数据模型有两种方式:按照外键映射和按照主键映射。
【1】基于外键映射的双向1-1
对于基于外键的1-1关联,其外键可以存放在任意一边,在需要存放外键一端,增加many-to-one元素。为many-to-one元素增加unique=“true” 属性来表示为1-1关联。
实例如下:
<!-- 使用 many-to-one 的方式来映射 1-1 关联关系 --> <!-- name:deparment中关联的manager的属性名; class:deparment中关联的manager的类型; column:数据表DEPARTMENTS中拥有的外键列MGR_ID unique:为外键列添加唯一索引 --> <many-to-one name="mgr" class="Manager" column="MGR_ID" unique="true"> </many-to-one>
另一端需要使用one-to-one元素,该元素使用 property-ref 属性指定使用被关联实体主键以外的字段作为关联字段。
实例如下:
<!-- 映射 1-1 的关联关系: 在对应的数据表中已经有外键了, 当前持久化类使用 one-to-one 进行映射 --> <!-- 没有外键的一端需要使用one-to-one元素,该元素使用 property-ref 属性指定使用被关联实体(department)主键以外的字段作为关联字段 --> <one-to-one name="dept" class="Department" property-ref="mgr"> <!-- 属性引用通常为外键列(外键列对应的字段) --> </one-to-one>
这里Department:Manager=主:从。
Department如下:
public class Department { private Integer deptId; private String deptName; private Manager mgr; public Integer getDeptId() { return deptId; } public void setDeptId(Integer deptId) { this.deptId = deptId; } public String getDeptName() { return deptName; } public void setDeptName(String deptName) { this.deptName = deptName; } public Manager getMgr() { return mgr; } public void setMgr(Manager mgr) { this.mgr = mgr; } @Override public String toString() { return "Department [deptId=" + deptId + ", deptName=" + deptName + ", mgr=" + mgr + "]"; } }
Department.hbm.xml如下:
<hibernate-mapping package="com.jane.one2one.foreign"> <class name="Department" table="DEPARTMENTS"> <id name="deptId" type="java.lang.Integer"> <column name="DEPT_ID" /> <generator class="native" /> </id> <property name="deptName" type="java.lang.String"> <column name="DEPT_NAME" /> </property> <!-- 使用 many-to-one 的方式来映射 1-1 关联关系 --> <!-- name:deparment中关联的manager的属性名; class:deparment中关联的manager的类型; column:数据表DEPARTMENTS中拥有的外键列MGR_ID unique:为外键列添加唯一索引 --> <many-to-one name="mgr" class="Manager" column="MGR_ID" unique="true"> </many-to-one> </class> </hibernate-mapping>
Manager类如下:
public class Manager { private Integer mgrId; private String mgrName; private Department dept; public Integer getMgrId() { return mgrId; } public void setMgrId(Integer mgrId) { this.mgrId = mgrId; } public String getMgrName() { return mgrName; } public void setMgrName(String mgrName) { this.mgrName = mgrName; } public Department getDept() { return dept; } public void setDept(Department dept) { this.dept = dept; } @Override public String toString() { return "Manager [mgrId=" + mgrId + ", mgrName=" + mgrName + ", dept=" + dept + "]"; } }
Manager.hbm.xml如下:
<hibernate-mapping package="com.jane.one2one.foreign"> <class name="Manager" table="MANAGERS"> <id name="mgrId" type="java.lang.Integer"> <column name="MGR_ID" /> <generator class="native" /> </id> <property name="mgrName" type="java.lang.String"> <column name="MGR_NAME" /> </property> <!-- 映射 1-1 的关联关系: 在对应的数据表中已经有外键了, 当前持久化类使用 one-to-one 进行映射 --> <!-- 这里注意,如果配置在对象获取的时候会出问题 --> <one-to-one name="dept" class="Department""></one-to-one> </class> </hibernate-mapping>
【2】代码测试
① 双向一对一持久化操作
代码如下:
@Test public void testSave(){ Department department = new Department(); department.setDeptName("DEPT-BB"); Manager manager = new Manager(); manager.setMgrName("MGR-BB"); //设定关联关系 department.setMgr(manager); manager.setDept(department); //保存操作 //建议先保存没有外键列的那个对象. 这样会减少 UPDATE 语句 session.save(department); session.save(manager); }
如果先保存有外键的对象,则会多出update语句;如果先保存没有外键的对象,则不会有额外的update语句产生。
如下所示:
Hibernate: insert into DEPARTMENTS (DEPT_NAME, MGR_ID) values (?, ?) Hibernate: insert into MANAGERS (MGR_NAME) values (?) Hibernate: update DEPARTMENTS set DEPT_NAME=?, MGR_ID=? where DEPT_ID=?
② 双向一对一获取
代码如下:
@Test public void testGet(){ //1. 默认情况下对关联属性使用懒加载 Department dept = (Department) session.get(Department.class, 1); System.out.println(dept.getDeptName()); //2. 所以会出现懒加载异常的问题. // session.close(); // Manager mgr = dept.getMgr(); // System.out.println(mgr.getClass()); // System.out.println(mgr.getMgrName()); //3. 查询 Manager 对象的连接条件应该是 dept.manager_id = mgr.manager_id //而不应该是 dept.dept_id = mgr.manager_id Manager mgr = dept.getMgr(); System.out.println(mgr.getMgrName()); }
如下所示:
Hibernate: select department0_.DEPT_ID as DEPT_ID1_1_0_, department0_.DEPT_NAME as DEPT_NAM2_1_0_, department0_.MGR_ID as MGR_ID3_1_0_ from DEPARTMENTS department0_ where department0_.DEPT_ID=? DEPT-BB Hibernate: select manager0_.MGR_ID as MGR_ID1_2_0_, manager0_.MGR_NAME as MGR_NAME2_2_0_, department1_.DEPT_ID as DEPT_ID1_1_1_, department1_.DEPT_NAME as DEPT_NAM2_1_1_, department1_.MGR_ID as MGR_ID3_1_1_ from MANAGERS manager0_ left outer join DEPARTMENTS department1_ on manager0_.MGR_ID=department1_.DEPT_ID //注意这里 where manager0_.MGR_ID=? MGR-BB
虽然同样查出来结果,但明显是数据巧合。这里的左外连接SQL有逻辑问题:
left outer join DEPARTMENTS department1_ on manager0_.MGR_ID=department1_.DEPT_ID
应该是dept.manager_id = mgr.manager_id 而不是dept.dept_id = mgr.manager_id!
这里就要说到博文开头的说明:
<!-- 没有外键的一端需要使用one-to-one元素,该元素使用 property-ref 属性指定使用被关联实体(department)主键以外的字段作为关联字段 --> <one-to-one name="dept" class="Department" property-ref="mgr"> </one-to-one>
如果one-to-one节点不配置property-ref属性,默认会使用dept主键进行关联对象。而配置了该属性后,如上所示,则会使用dept.mgr在数据库对应的列(manager_id)来对应!
测试测试结果如下:
Hibernate: select department0_.DEPT_ID as DEPT_ID1_1_0_, department0_.DEPT_NAME as DEPT_NAM2_1_0_, department0_.MGR_ID as MGR_ID3_1_0_ from DEPARTMENTS department0_ where department0_.DEPT_ID=? DEPT-BB Hibernate: select manager0_.MGR_ID as MGR_ID1_2_0_, manager0_.MGR_NAME as MGR_NAME2_2_0_, department1_.DEPT_ID as DEPT_ID1_1_1_, department1_.DEPT_NAME as DEPT_NAM2_1_1_, department1_.MGR_ID as MGR_ID3_1_1_ from MANAGERS manager0_ left outer join DEPARTMENTS department1_ on manager0_.MGR_ID=department1_.MGR_ID where manager0_.MGR_ID=? Hibernate: select department0_.DEPT_ID as DEPT_ID1_1_0_, department0_.DEPT_NAME as DEPT_NAM2_1_0_, department0_.MGR_ID as MGR_ID3_1_0_ from DEPARTMENTS department0_ where department0_.MGR_ID=? MGR-BB
另外需要注意的是,在查询没有外键的实体对象时, 使用的左外连接查询, 一并查询出其关联的对象并已经进行初始化。
代码如下:
@Test public void testGet2(){ Manager mgr = (Manager) session.get(Manager.class, 1); System.out.println(mgr.getMgrName()); System.out.println(mgr.getDept().getDeptName()); }
测试结果如下:
Hibernate: select manager0_.MGR_ID as MGR_ID1_2_0_, manager0_.MGR_NAME as MGR_NAME2_2_0_, department1_.DEPT_ID as DEPT_ID1_1_1_, department1_.DEPT_NAME as DEPT_NAM2_1_1_, department1_.MGR_ID as MGR_ID3_1_1_ from MANAGERS manager0_ left outer join DEPARTMENTS department1_ on manager0_.MGR_ID=department1_.MGR_ID where manager0_.MGR_ID=? MGR-BB DEPT-BB
【3】基于主键映射的双向一对一
基于主键的映射策略:指一端的主键生成器使用 foreign 策略,表明根据”对方”的主键来生成自己的主键,自己并不能独立生成主键。 <param>
子元素指定使用当前持久化类的哪个属性作为 “对方”。
<id name="deptId" type="java.lang.Integer"> <column name="DEPT_ID" /> <!-- 使用外键的方式来生成当前的主键 --> <generator class="foreign"> <!-- property 属性指定使用当前持久化类的哪一个属性的主键作为外键 --> <param name="property">mgr</param> </generator> </id>
采用foreign主键生成器策略的一端增加 one-to-one 元素映射关联属性,其one-to-one属性还应增加 constrained=“true” 属性;另一端增加one-to-one元素映射关联属性。
<!-- 采用 foreign 主键生成器策略的一端增加 one-to-one 元素映射关联属性, 其 one-to-one 节点还应增加 constrained=true 属性, 以使当前的主键上添加外键约束 --> <one-to-one name="mgr" class="Manager" constrained="true"></one-to-one>
constrained(约束):指定为当前持久化类对应的数据库表的主键添加一个外键约束,引用被关联的对象(“对方”)所对应的数据库表主键。
修改Department.hbm.xml如下:
<hibernate-mapping package="com.jane.one2one.primary"> <class name="Department" table="DEPARTMENTS"> <id name="deptId" type="java.lang.Integer"> <column name="DEPT_ID" /> <!-- 使用外键的方式来生成当前的主键 --> <generator class="foreign"> <!-- property 属性指定使用当前持久化类的哪一个属性的主键作为外键 --> <param name="property">mgr</param> </generator> </id> <property name="deptName" type="java.lang.String"> <column name="DEPT_NAME" /> </property> <!-- 采用 foreign 主键生成器策略的一端增加 one-to-one 元素映射关联属性, 其 one-to-one 节点还应增加 constrained=true 属性, 以使当前的主键上添加外键约束 --> <one-to-one name="mgr" class="Manager" constrained="true"></one-to-one> </class> </hibernate-mapping>
修改Manager.hbm.xml如下:
<hibernate-mapping package="com.jane.one2one.primary"> <class name="Manager" table="MANAGERS"> <id name="mgrId" type="java.lang.Integer"> <column name="MGR_ID" /> <generator class="native" /> </id> <property name="mgrName" type="java.lang.String"> <column name="MGR_NAME" /> </property> <one-to-one name="dept" class="Department"></one-to-one> </class> </hibernate-mapping>
【4】代码测试
① 持久化操作
此时先插入哪一个都不会有多余的 UPDATE,程序总会先插入Manager,因为Department主键是根据Manager主键生成的。
测试代码如下:
@Test public void testSave(){ Department department = new Department(); department.setDeptName("DEPT-DD"); Manager manager = new Manager(); manager.setMgrName("MGR-DD"); //设定关联关系 manager.setDept(department); department.setMgr(manager); //保存操作 //先插入哪一个都不会有多余的 UPDATE session.save(department); session.save(manager); }
测试结果如下:
Hibernate: create table DEPARTMENTS ( DEPT_ID integer not null, DEPT_NAME varchar(255), MGR_ID integer, primary key (DEPT_ID) ) engine=InnoDB Hibernate: create table MANAGERS ( MGR_ID integer not null auto_increment, MGR_NAME varchar(255), primary key (MGR_ID) ) engine=InnoDB Hibernate: alter table DEPARTMENTS drop index UK_hxcnjqu282dceadn2uqf7sc5 Hibernate: alter table DEPARTMENTS add constraint UK_hxcnjqu282dceadn2uqf7sc5 unique (MGR_ID) Hibernate: alter table DEPARTMENTS add constraint FKmd738stvsm76ss2glhif6ljdv foreign key (DEPT_ID) references MANAGERS (MGR_ID) Hibernate: alter table DEPARTMENTS add constraint FKh0nupqefmu52d6n1ny0vq9g9k foreign key (MGR_ID) references MANAGERS (MGR_ID) Hibernate: insert into MANAGERS (MGR_NAME) values (?) Hibernate: insert into DEPARTMENTS (DEPT_NAME, DEPT_ID) values (?, ?)
② 对象获取
测试代码一如下:
@Test public void testGet(){ //1. 默认情况下对关联属性使用懒加载 Department dept = (Department) session.get(Department.class, 1); System.out.println(dept.getDeptName()); // session.close(); //2. 如果session关闭则会会出现懒加载异常的问题. Manager mgr = dept.getMgr(); System.out.println(mgr.getMgrName()); }
测试结果如下:
Hibernate: select department0_.DEPT_ID as DEPT_ID1_1_0_, department0_.DEPT_NAME as DEPT_NAM2_1_0_ from DEPARTMENTS department0_ where department0_.DEPT_ID=? DEPT-DD Hibernate: select manager0_.MGR_ID as MGR_ID1_2_0_, manager0_.MGR_NAME as MGR_NAME2_2_0_, department1_.DEPT_ID as DEPT_ID1_1_1_, department1_.DEPT_NAME as DEPT_NAM2_1_1_ from MANAGERS manager0_ left outer join DEPARTMENTS department1_ on manager0_.MGR_ID=department1_.DEPT_ID //这里注意,主键对应 where manager0_.MGR_ID=? MGR-DD
测试代码二如下:
@Test public void testGet2(){ //在查询没有外键的实体对象时, 使用的左外连接查询, 一并查询出其关联的对象 //并已经进行初始化. Manager mgr = (Manager) session.get(Manager.class, 1); System.out.println(mgr.getMgrName()); System.out.println(mgr.getDept().getDeptName()); }
测试结果如下:
Hibernate: select manager0_.MGR_ID as MGR_ID1_2_0_, manager0_.MGR_NAME as MGR_NAME2_2_0_, department1_.DEPT_ID as DEPT_ID1_1_1_, department1_.DEPT_NAME as DEPT_NAM2_1_1_ from MANAGERS manager0_ left outer join DEPARTMENTS department1_ on manager0_.MGR_ID=department1_.DEPT_ID // 这里主键对应 where manager0_.MGR_ID=? MGR-DD DEPT-DD