如需转载,请说明出处: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. Hibernate 对象关系映射文件

    简介: POJO 类和关系型数据库之间的映射可以用一个 XML 文档来定义 通过 POJO 类的数据库映射文件,Hibernate 可以理解持久化类和数据表之间的对应关系,也可以理解持久化类属性与数据 ...

  2. hibernate对象关系映射( 一对一,一对多,多对一,多对多的单向,双向映射 ——)

    对象之间的关系: 关系映射之间的关系只的是对象之间的关系,并不指数据库表的关系(外键关系)这儿解决的问题是当对象之间的关系之一时,数据库表该如何映射,编程上如何对待. 一对一(主键关联,和单向的外键关 ...

  3. hibernate对象关系实现(一)一对多

    hibernate是对jdk一个封装工具,实现对象和数据库之间数据映射.使用时涉及到四个问题:a.对象之间的关系在类中的体现:b,对象关系对应的数据库中表之间体现:c.实现a,b在hibernate的 ...

  4. hibernate对象关系映射的配置

    一对一主键关联单双向 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-m ...

  5. Hibernate -- 对象关系映射基础

  6. Hibernate学习笔记三:对象关系映射(一对一,一对多,多对一,多对多)

    如需转载,请说明出处:http://www.cnblogs.com/gudu1/p/6895610.html Hibernate通过关系映射来表示数据库中表与表之间的关系,关系映射可以通过两种方式:配 ...

  7. MyBatis加强(1)~myBatis对象关系映射(多对一关系、一对多关系)、延迟/懒加载

    一.myBatis对象关系映射(多对一关系.一对多关系) 1.多对一关系: ---例子:多个员工同属于一个部门. (1)myBatis发送 额外SQL: ■ 案例:员工表通过 dept_id 关联 部 ...

  8. Hibernate (开源对象关系映射框架)

    一.基本介绍1.它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm(对象关系映射)框架,hibernate可以自动生成SQL语句,自动执行: Hibern ...

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

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

随机推荐

  1. CentOS7防火墙设置常用命令

    目录 开/关/重启防火墙 查看所有开启的端口号 CentOS7环境下防火墙常用命令 开/关/重启防火墙 查看防火墙状态 firewall-cmd --state 启动防火墙 systemctl sta ...

  2. 在C#中使用RESTful API的几种好方法

    什么是Restful API REST 即Representational State Transfer的缩写.直接翻译的意思是"表现层状态转化". 它是一种互联网应用程序的API ...

  3. C#设计模式学习笔记:简单工厂模式(工厂方法模式前奏篇)

    本笔记摘抄自:https://www.cnblogs.com/PatrickLiu/p/7551373.html,记录一下学习过程以备后续查用. 一.引言 简单工厂模式并不属于GoF23里面的设计模式 ...

  4. Spring-Framework-官方文档阅读(一)Spring IoC Container

    通读Spring IoC容器官方文档,对IoC容器有一个大致的了解. 环境 JDK1.8 Spring Framework Version :4.3.18.RELEASE 容器概述 接口org.spr ...

  5. windows密码抓取工具-mimikatz

    前言 介绍一下windows的密码hash值的组成: Windows系统下的hash密码格式为:用户名称:RID:LM-HASH值:NT-HASH值,例如: Administrator::C8825D ...

  6. 简单记账本APP开发一

    在对Android的一些基础的知识有了一定了解,以及对于AndroidStudio的如何使用有了 一定的熟悉后,决定做一个简单的记账本APP 开发流程 1.记账本的页面 2.可以添加新的账目 (一)页 ...

  7. Win10如何设置休眠选项(关于睡眠、休眠、快速启动这几个伪关机功能如何设置更适合笔记本电脑?)

    · Win10如何设置休眠选项(关于睡眠.休眠.快速启动这几个伪关机功能如何设置更适合笔记本电脑?) 应用场景 升级正式版win10以后,发现竟然没有休眠选项,从电源管理器里面也没有找到,有时候有些重 ...

  8. Verilog-case、casez和casex的区别

    参考博客:https://www.cnblogs.com/guolongnv/articles/6906929.html 1.基本概念 1)?表示z,而不是“dont care” 2)区分: case ...

  9. linux基础之CnetOS安装

    CentOS启动流程 POST-->boot sequence(bios)--> bootloader(mbr)-->kernel(ramdisk)-->rootfs(ro)- ...

  10. 原生js判断设备类型

    var u = navigator.userAgent; //Android终端 var isAndroid = u.indexOf('Android') > -1 || u.indexOf(' ...