一、唯一外键关联映射: (每个员工只属于一个部门)
唯一外键关联生成的关系模型:
t_employee(id int pk, name varchar, departmentid int fk->t_department(id))
t_department(id int pk, name varchar)
1、唯一外键单向关联
实体模型:
com.lrh.hibernate.Employee(int id, String name, Department department)
com.lrh.hibernate.Department(int id, String name)
配置文件:
<!-- com.lrh.hibernate/Employee.hbm.xml -->
<hibernate-mapping>
<class name="com.lrh.hibernate.Employee" table="t_employee">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<!-- 为<many-to-one>加上unique就变成了一对一 -->
<many-to-one name="department" unique="true" column="departmentid"/>
</class>
</hibernate-mapping>
<!-- com.lrh.hibernate/Department.hbm.xml -->
<hibernate-mapping>
<class name="com.lrh.hibernate.Department" table="t_department">
<id name="id"><generator class="native"/></id>
<property name="name"/>
</class>
</hibernate-mapping>
2、唯一外键双向关联
实体模型:
com.lrh.hibernate.Employee(int id, String name, Department department)
com.lrh.hibernate.Department(int id, String name, Employee employee)
配置文件:
<!-- com.lrh.hibernate/Employee.hbm.xml -->
<hibernate-mapping>
<class name="com.lrh.hibernate.Employee" table="t_employee">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<!-- <many-to-one>加上unique='true'表示一对一 -->
<many-to-one name="department" unique="true" column="departmentid"/>
</class>
</hibernate-mapping>
<!-- com.lrh.hibernate/Department.hbm.xml -->
<hibernate-mapping>
<class name="com.lrh.hibernate.Department" table="t_department">
<id name="id"><generator class="native"/></id>
<property name="name"/>
<!--
只需要在另一端(department)加入<one-to-one>标签,但是hibernate默认按照主键加载,所以要指示hibernate按照指定列来加载
-->
<one-to-one name="employee" property-ref="department"/>
</class>
</hibernate-mapping>
分享到:
相关推荐
NULL 博文链接:https://huihai.iteye.com/blog/1243558
博文链接:https://shaqiang32.iteye.com/blog/201311
博文链接:https://shaqiang32.iteye.com/blog/201312
博文链接:https://llying.iteye.com/blog/220804
hibernate one-to-one 一对一唯一外键关联映射_单向 and 双向
NULL 博文链接:https://huihai.iteye.com/blog/1243530
Hibernate 一对一外键单向关联 Hibernate 一对一主键单向关联 Hibernate 一对一连接表单向关联 Hibernate 一对多外键单向关联 Hibernate 一对多连接表单向关联 Hibernate 多对一外键单向关联 Hibernate 多对一连接...
NULL 博文链接:https://12345678.iteye.com/blog/723871
NULL 博文链接:https://zhouhaitao.iteye.com/blog/1171227
博文链接:https://llying.iteye.com/blog/220807
NULL 博文链接:https://dreamzhong.iteye.com/blog/1199508
Hibernate关联关系映射 单向关联 │ ├─ 一对一外键单向关联 │ ├─ 一对一主键单向关联 │ ├─ 一对一连接表单向关联 │ ├─ 一对多外键单向关联 │ ├─ 一对多连接表单向关联 │ ├─ 多对一外键单向关联 │ ...
http://blog.csdn.net/e421083458/article/details/8794127 该源码为Hibernate教程配套源码
专用于HIBERNATE中两个对象间的外键关联查询
http://blog.csdn.net/e421083458/article/details/8794127 该源码为Hibernate教程配套源码
主要介绍了浅谈hibernate急迫加载问题(多重外键关联),具有一定借鉴价值,需要的朋友可以参考下。