实现方式
Department表的主键作为外键关联到Manager表,让两个表中对应记录的主键一致
持久化类不变
创建映射文件
Department
<class name ="Department" table="DEPARTMENTS">
<id name ="deptId" type="java.lang.Integer">
<column name ="DEPT_ID" />
<!-- 设置当前表的主键列依赖另外一张表的主键实现 -->
<generator class ="foreign">
<param name ="property">manager</ param>
</generator >
</id >
<property name ="deptName" type="java.lang.String">
<column name ="DEPT_NAME" />
</property >
<!-- 使用constrained属性给生成的主键列加上外键约束 -->
<one-to-one name ="manager" class="Manager" constrained= "true" />
</class >
Manager
<class name ="Manager" table="MANAGERS">
<id name ="mngId" type="java.lang.Integer">
<column name ="MNG_ID" />
<generator class ="native" />
</id >
<property name ="mngName" type="java.lang.String">
<column name ="MNG_NAME" />
</property >
<one-to-one name ="department" class="Department" />
</class >