如需转载,请说明出处:http://www.cnblogs.com/gudu1/p/6895610.html

Hibernate通过关系映射来表示数据库中表与表之间的关系,关系映射可以通过两种方式:配置文件映射和注解映射,本文主要讲解配置文件映射。

  关系映射:

    1.单向关联关系,表示只有一方维护关系,而另一方并不知道有这种关系,在解除和删除关联关系的时候也要在有外键方进行,否则会抛出异常,因为有外键的约束。

    2.双向关联关系。双方都维护关系,无论在哪一方解除和删除关联关系,Hibernate都能够获知,都能够正确的进行操作。

  关系映射中,即使你在类中添加了属性(即相关联的类对象),通过配置inverse属性,也可以设置它是否维护关系,默认为true,可以设置为false表示不维护

一:单向关联

单向多对一:

  单向(many-to-one)是最常见的单向关联关系: 

<class name="Department" table="department">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="uname" column="uname" type="string" length="20"></property>
</class> <class name="Employee" table="employee">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="uname" type="string" length="20" column="uname"></property>
<many-to-one name="department" class="Department" column="department_id"></many-to-one>
</class> 

  下面是对应数据库表 

 

单向一对多:

  基于外键关联的单向一对多关联是一种很少见的情况,不推荐使用它。

<class name="Department" table="department">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="uname" column="uname" type="string" length="20"></property>
<set name="employeeSet">
<key column="employee_id"></key>
<one-to-many class="Employee" />
</set>
</class> <class name="Employee" table="employee">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="uname" type="string" length="20" column="uname"></property>
</class>

  下面是对应数据库表,可以看出跟上面是一样的

单向一对一:  

  基于外键关联的单向一对一关联和单向多对一关联几乎是一样的。唯一的不同就是单向一对一关联中的外键字段具有唯一性约束(unique)。

<class name="Department" table="department">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="uname" column="uname" type="string" length="20"></property>
</class> <class name="Employee" table="employee">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="uname" type="string" length="20" column="uname"></property>
<many-to-one name="department" column="department_id" class="Department" unique="true"></many-to-one>
</class>

  看图好像和(一对多|多对一)结构一样,确实是一样的,你可以插几条数据试一下,因为是一对一关系,有可能会出错的哦!

二:使用连接表单向关联

  单向一对多:

    基于连接表的单向一对多关联 应该优先被采用。请注意,通过指定unique="true",我们可以把多样性从多对多改变为一对多。

<class name="Department" table="department">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="uname" column="uname" type="string" length="20"></property>
<set name="employeeSet" table="empl_depa">
<key column="department_id"></key>
<many-to-many column="employee_id" class="Employee" unique="true"></many-to-many>
</set>
</class> <class name="Employee" table="employee">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="uname" type="string" length="20" column="uname"></property>
</class>

  

  单向多对一:

    基于连接表的单向多对一关联在关联关系可选的情况下应用也很普遍。

<class name="Employee" table="employee">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="uname" type="string" length="20" column="uname"></property>
<join table="emplo_depa">
<key column="employee_id"></key>
<many-to-one name="department" class="Department" column="department_id"></many-to-one>
</join>
</class> <class name="Department" table="department">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="uname" column="uname" type="string" length="20"></property> </class>

  

  单向一对一:

  基于连接表的单向一对一关联也是可行的,但非常少见。

<class name="Department" table="department">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="uname" column="uname" type="string" length="20"></property>
</class> <class name="Employee" table="employee">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="uname" type="string" length="20" column="uname"></property>
<join table="emplo_depa">
<key column="employee_id"></key>
<many-to-one name="department" class="Department" column="department_id" unique="true"></many-to-one>
</join>
</class>

  

  单向多对多:

<class name="Department" table="department">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="uname" column="uname" type="string" length="20"></property>
<set name="employeeSet" table="emplo_depa">
<key column="department_id"></key>
<many-to-many class="Employee" column="employee_id"></many-to-many>
</set>
</class> <class name="Employee" table="employee">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="uname" type="string" length="20" column="uname"></property>
</class>

  

三:双向关联

  一对一双向关联

<class name="Department" table="department">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="uname" column="uname" type="string" length="20"></property>
<one-to-one name="employee" class="Employee" property-ref="department"></one-to-one>
</class> <class name="Employee" table="employee">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="uname" type="string" length="20" column="uname"></property>
<many-to-one name="department" class="Department" unique="true"></many-to-one>
</class>

  

  一对多双向关联

<class name="Department" table="department">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="uname" column="uname" type="string" length="20"></property>
<set name="employeeSet">
<key column="department_id"></key>
<one-to-many class="Employee" />
</set>
</class> <class name="Employee" table="employee">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="uname" type="string" length="20" column="uname"></property>
<many-to-one name="department" class="Department" column="department_id"></many-to-one>
</class>

  

  双向多对一关联把上面权限反转一下就好,这里就不贴代码了。

四:使用连接表双向关联

  一对一双向关联

  

<class name="Department" table="department">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="uname" column="uname" type="string" length="20"></property>
<join table="emplo_depa" inverse="true" optional="true">
<key column="department_id" unique="true"></key>
<many-to-one name="employee" unique="true" column="employee_id"></many-to-one>
</join>
</class> <class name="Employee" table="employee">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="uname" type="string" length="20" column="uname"></property>
<join table="emplo_depa" optional="true">
<key column="employee_id" unique="true"></key>
<many-to-one name="department" unique="true" column="department_id"></many-to-one>
</join>
</class>

   

  多对多双向关联

<class name="Department" table="department">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="uname" column="uname" type="string" length="20"></property>
<set name="employeeSet" table="emplo_depa" inverse="true">
<key column="department_id"></key>
<many-to-many class="Employee" column="employee_id" ></many-to-many>
</set>
</class> <class name="Employee" table="employee">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="uname" type="string" length="20" column="uname"></property>
<set name="departmentSet" table="emplo_depa">
<key column="employee_id"></key>
<many-to-many class="Department" column="department_id"></many-to-many>
</set>
</class>

  

  一对多双向关联

<class name="Department" table="department">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="uname" column="uname" type="string" length="20"></property>
<set name="employeeSet" inverse="true" table="emplo_depa">
<key column="department_id"></key>
<many-to-many unique="true" class="Employee" column="employee_id"></many-to-many>
</set>
</class> <class name="Employee" table="employee">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="uname" type="string" length="20" column="uname"></property>
<join table="emplo_depa">
<key column="employee_id"></key>
<many-to-one name="department" column="department_id" class="Department"></many-to-one>
</join>
</class>

除此之外,还有更多映射配置(继承映射,组件映射),本篇就不写那么多了,希望读者能够查阅文件学习。每天一小步,人生一大步。

  

  

Hibernate学习笔记三:对象关系映射(一对一,一对多,多对一,多对多)的更多相关文章

  1. [原创]java WEB学习笔记81:Hibernate学习之路--- 对象关系映射文件(.hbm.xml):hibernate-mapping 节点,class节点,id节点(主键生成策略),property节点,在hibernate 中 java类型 与sql类型之间的对应关系,Java 时间和日期类型的映射,Java 大对象类型 的 映射 (了解),映射组成关系

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  2. Hibernate学习笔记二:常用映射配置

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6760895.html 一:单向一对一 常用唯一外键的方法来配置单向一对一关系. 1:实体关系 类A中有类B对象 ...

  3. Hibernate学习笔记三 多表

    一对多|多对一 表中的表达 实体中的表达 实体代码: package com.yyb.domain; import java.util.HashSet; import java.util.Set; p ...

  4. JPA学习笔记(8)——映射双向一对多关联关系

    双向一对多关联关系 前面的博客讲的都是单向的,而本问讲的是双向的(双向一对多 = 双向多对一) 什么是双向? 我们来对照一下单向和双向 单向/双向 User实体类中是否有List< Order& ...

  5. Hibernate学习笔记三

    1.1.1 Hibernate的关联关系映射:(多对多) 1.1.1.1 多对多的配置: 步骤一创建实体和映射: Student: public class Student { private Int ...

  6. JS学习笔记 (三) 对象进阶

    1.JS对象 1.1 JS对象特征 1.JS对象是基本数据数据类型之一,是一种复合值,可以看成若干属性的集合. 属性是名值对的形式(key:value) 属性名是字符串,因此可以把对象看成是字符串到值 ...

  7. Hibernate学习笔记三:常用数据库操作语句

    转载请注明原文地址: 一:HQL 1:HQL语句格式:select from POJO类名 where 条件表达式 group by 属性 having 聚集函数 order by 属性 [其中,fr ...

  8. Hibernate映射--基本类映射和对象关系映射(转)

    原文地址:http://blog.csdn.net/lovesummerforever/article/details/20901011   尊重原创,请访问原网址 回想一些我们在没有学习ssh的时候 ...

  9. Hibernate的核心对象关系映射

    Hibernate的核心就是对象关系映射: 加载映射文件的两种方式: 第一种:<mapping resource="com/bie/lesson02/crud/po/employee. ...

随机推荐

  1. 微信小程序,前端大梦想(一)

    小程序框架MINA简介       微信公众平台"小程序"具有不是APP胜似APP的效果,是一种不需要下载安装即可使用的应用,它实现了应用"触手可及"的梦想,用 ...

  2. Python3.5爬虫统计AcFun所有视频,并按各个类别进行Top100排序展示

    前(b)言(b): 前段时间对Python产生了浓厚的兴趣,所以决定入门学习了1个多月,后来某时我需要对tomcat做一个压力测试,于是我想到了用Python写一个压力测试的脚本吧!最后捣鼓出了一个脚 ...

  3. Redis数据类型之列表List

    Redis列表简介 Redis列表是简单的字符串列表,一个列表最多可以包含 232 - 1 个元素.列表按照插入顺序排序,可以从列表的头部或者尾部添加元素 上图演示了使用LPUSH向列表中插入元素,并 ...

  4. Java线程池(ThreadPool)详解

    线程五个状态(生命周期): 线程运行时间 假设一个服务器完成一项任务所需时间为:T1 创建线程时间,T2 在线程中执行任务的时间,T3 销毁线程时间.    如果:T1 + T3 远大于 T2,则可以 ...

  5. 如何禁止火狐onblur时alert()产生类似选中的拖蓝效果

    输入框中onblur 然后alert();会产生 复制 选中的效果的效果( 拖蓝) onblur="aa()"function aa(){ alert("--" ...

  6. 读《effective C++》1

    条款一:视C++为一个语言联邦 学习C++半个月了,学了他的面向过程编程,面向对象编程(封装性,继承性,多态性),template泛型编程,开始只是觉得C++基础是面向对象,但是学了这么多块开始有点迷 ...

  7. JDK与Apache Tomcat服务器的安装步骤

    先解释一下JDK和Tomcat是什么: Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP ...

  8. easyUI中datagrid的使用

    easyUI中的datagrid数据表格经常被用到,结合项目中的使用情况,总结一下datagrid使用中需要注意的一些问题.使用datagrid展示数据,需要在html.css.js中都要编写代码,h ...

  9. JVM 堆内存,参数优化

    Java堆内存 http://www.importnew.com/19593.html JVM诊断之查看运行参数 JVM 垃圾回收器工作原理及使用实例介绍 https://www.ibm.com/de ...

  10. 【转】为什么delete以后指针还能被赋值

    首先,系统知道哪一部分堆的线性空间被占掉了,new就是起这个作用,仅仅是声明一下(可能多了一个功能),因为堆的空间不一定是直接从系统调用获得的,堆的空间是这样管理的:程序先伸请一个大的堆空间,这个时候 ...