一、单向关联(unidirectional associations):

1.1.1 Many-to-one

Employee.hbm.xml
<class name="Employee" table="T_EMPLOYEE">
<id name="employee_Id" column="EMPLOYEE_ID">
<generator class="native"/>
</id>
<many-to-one name="department" column="DEPARTMENT_ID" not-null="true"/>
</class>
Department.hbm.xml
<class name="Department" table="T_DEPARTMENT"> <id name="department_Id" column="DEPARTMENT_ID"> <generator class="native"/> </id> </class> create table EMPLOYEE(employee_Id bigint not null primary key,department_Id bigint not null) create table DEPARTMENT(department_Id bigint not null primary key)

?单向的<many-to-one>里没有指明class

1.1.2 One-t-one

1>单向一对一和单向多对一区别就是Unique这个属性,基于外键

<class name="Employee" table="T_EMPLOYEE">
<id name="employee_Id" column="EMPLOYEE_ID">
<generator class="native"/>
</id>
<many-to-one name="department"
column="DEPARTMENT_ID"
unique="true"
not-null="true"/>
</class> <class name="Department" table="T_DEPARTMENT">
<id name="department_Id" column="DEPARTMENT_ID">
<generator class="native"/>
</id>
</class>

2>基于主键

<class name="Person" table="Person">
<id name="Person_Id" column="PERSON_ID">
<generator class="native"/>
</id>
</class> <class name="Card" table="Card">
<id name="Card_Id" column="PERSON_ID">
<generator class="foreign">
<param name="property">person</param>
</generator>
</id>
<one-to-one name="person" constrained="true"/>
</class> create table Person ( PERSON_ID bigint not null primary key )
create table Card( PERSON_ID bigint not null primary key )

? one-to-one 没有column和table属性,有class属性,name标志?,如何指定person对象。

1.1.3 One-to-many

<class name="Department">
<id name="department_Id" column="DEPARTMENT_ID">
<generator class="native"/>
</id>
<set name="employee">
<key column="DEPARTMENT_ID"
not-null="true"/>
<one-to-many class="Employee"/>
</set>
</class> <class name="Employee">
<id name="employee_Id" column="EMPLOYEE_ID">
<generator class="native"/>
</id>
</class> create table Department( department_Id bigint not null primary key )
create table Emplyee( employee_Id bigint not null primary key, personId bigint not null )

you should instead use a join table for this kind of asociation

二、单向关联with join table

2.1.1 One-to-many

<class name="Department">
<id name="department_Id" column="DEPARTMENT_ID">
<generator class="native"/>
</id>
<set name="employees" table="DepartmetEmployee">
<key column="DEPARTMENT_ID"/>
<many-to-many column="EMPLOYEE_ID"
unique="true"
class="Employee"/>
</set>
</class> <class name="Employee">
<id name="employee_Id" column="EMPLOYEE_ID">
<generator class="native"/>
</id>
</class> create table Department( Department_Id bigint not null primary key )
create table DepartmentEmployee(Department_Id bigint not null, employee_Id bigint not null primary key )
create table Employee( employee_Id bigint not null primary key )

specifying unique="ture" change multiplicity from many-to-many to one-to-many.

2.1.2.Many-to-one

<class name="Department">
<id name="department_Id" column="DEPARTMENT_ID">
<generator class="native"/>
</id>
</class> <class name="Employee">
<id name="employee_Id" column="EMPLOYEE_ID">
<generator class="native"/>
</id>
<join table="EmployeeDepartment"
optional="true">
<key column="EMPLOYEE_ID" unique="true"/>
<many-to-one name="department"
column="DEPARTMENT_ID"
not-null="true"/>
</join>
</class>

2.1.3 One-to-one

<class name="Person" table="Person">
<id name="Person_Id" column="PERSON_ID">
<generator class="native"/>
</id>
<join table="PersonCard"
optional="true">
<key column="PERSON_ID"
unique="true"/>
<many-to-one name="card"
column="CARD_ID"
not-null="true"
unique="true"/>
</join>
</class> <class name="Card" table="Card">
<id name="Card_Id" column="CARD_ID">
<generator class="native"/>
</id>
<one-to-one name="person" constrained="true"/>
</class> create table Person ( PERSON_ID bigint not null primary key )
create table PersonCard ( PERSON_ID bigint not null primary key, CARD_ID bigint not null unique )
create table Card( CARD_ID bigint not null primary key )

2.1.4.Many-to-many

<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<set name="addresses" table="PersonAddress">
<key column="personId"/>
<many-to-many column="addressId"
class="Address"/>
</set>
</class> <class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
</class> create table Person ( personId bigint not null primary key )
create table PersonAddress ( personId bigint not null, addressId bigint not null, primary key (personId, addressId) )
create table Address ( addressId bigint not null primary key )

三、双向关联(Bidirectional associations)

3.1.1.one-to-one

1>

<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<one-to-one name="address"/>
</class> <class name="Address">
<id name="id" column="personId">
<generator class="foreign">
<param name="property">person</param>
</generator>
</id>
<one-to-one name="person"
constrained="true"/>
</class>
create table Person ( personId bigint not null primary key )
create table Address ( personId bigint not null primary key )

2>

<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<many-to-one name="address"
column="addressId"
unique="true"
not-null="true"/>
</class> <class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
<one-to-one name="person"
property-ref="address"/>
</class> create table Person ( personId bigint not null primary key, addressId bigint not null unique )
create table Address ( addressId bigint not null primary key )

3.1.2.one-to-many/many-to-one:

1>

<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<many-to-one name="address"
column="addressId"
not-null="true"/>
</class> <class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
<set name="people" inverse="true">
<key column="addressId"/>
<one-to-many class="Person"/>
</set>
</class> create table Person ( personId bigint not null primary key, addressId bigint not null )
create table Address ( addressId bigint not null primary key )

2>

<class name="Person">
<id name="id"/>
...
<many-to-one name="address"
column="addressId"
not-null="true"
insert="false"
update="false"/>
</class> <class name="Address">
<id name="id"/>
...
<list name="people">
<key column="addressId" not-null="true"/>
<list-index column="peopleIdx"/>
<one-to-many class="Person"/>
</list>
</class>

四、双向关联with join table

4.1.1.one-to-many/many-to-one

4.1.2.one-to-one

4.1.3.many-to-many

hibernate ORM related的更多相关文章

  1. wildfly 10上使用最新的 Hibernate ORM OGM

    ORM是关系型数据库连接:ogm是No sql数据库连接,Mongo, redis等. 1,下载ogm zip包,解压到wildfly_home\modules\system\layers\base, ...

  2. [JavaEE] Hibernate ORM

    一. Hibernate的简要介绍 Hibernate是轻量级Java EE应用的持久层解决方案,Hibernate不仅管理者Java类到数据库表的映射(包括Java 数据类型到SQL数据类型的映射) ...

  3. Hibernate (ORM)

    1 框架体系结构 2 hibernate入门 2.1 ORM框架 Hibernate是一个数据持久化层的ORM框架. Object:对象,java对象,此处特指JavaBean Relational: ...

  4. Spring ORM数据訪问——Hibernate

    Hibernate 我们将首先介绍Spring环境中的Hibernate 5.然后介绍使用Hibernate 5来演示Spring集成O-R映射器的方法. 本节将具体介绍很多问题,并显示DAO实现和事 ...

  5. Hibernate Validator 6.0.9.Final - JSR 380 Reference Implementation: Reference Guide

    Preface Validating data is a common task that occurs throughout all application layers, from the pre ...

  6. JSP利用Hibernate实现对数据库的CRUD ——开发环境Myeclipse与SQL Server 2008

    一.首先先建立一个Web Project 二.然后在程序根目录建立文件夹“DataBase”和“Doc”,分别存放数据库文件和保存SQL语句,建完如下所示: 三.建立数据库“dbHibernate”, ...

  7. hibernate入门实例

    1. 环境配置 1.1 hiberante环境配置 hibernate可实现面向对象的数据存储.hibernate的官网:http://hibernate.org/ 官网上选择hibernate OR ...

  8. hibernate(1)

    1. 环境配置 1.1 hiberante环境配置 hibernate可实现面向对象的数据存储.hibernate的官网:http://hibernate.org/ 官网上选择hibernate OR ...

  9. 如何下载Hibernate

    官网: http://hibernate.org/ 打开hibernate官网,选择Hibernate ORM,点击左侧的Downloads 点击Downloads后,可以看到如下页面,右侧是各个版本 ...

随机推荐

  1. 《Python机器学习》笔记(五)

    通过降维压缩数据 在前面已经介绍了几种不同的特征选择技术对数据集进行降维的方法.另一种常用于降维的特征选择方法就是特征抽取.数据压缩也是机器学习领域中的一个重要内容.数据压缩技术可以帮助我们对数据及逆 ...

  2. Spring学习笔记5—为Spring添加REST功能

    1 关于REST 我的理解,REST就是将资源以最合适的形式在服务端和客户端之间传递. 系统中资源采用URL进行标识(可以理解为URL路径中带参数) 使用HTTP方法进行资源的管理(GET,PUT,P ...

  3. 鸟哥的Linux私房菜-第一部分-第2章Linux如何学习

    第2章 Linux如何学习 Linux可以干什么 企业级:网络服务器.金融数据库.大型企业网管环境.高性能计算.集群 个人:桌面计算机.手机.PDA(掌上电脑,这个电脑的意义十分广泛,在不同的场景下有 ...

  4. iOS 快速遍历 效率分析 for loop for in enumerateBlock 适用条件

    test1 简单遍历 结论: 当数组数据量很小 时候 for loop 和 for in 效率不相上下,随着数据量增长for in 快速枚举的优势 明显 如果需要知道 索引可用 enumrateBlo ...

  5. 快速查找文件——Everything

    Everything Search Engine Locate files and folders by name instantly. Small installation file Clean a ...

  6. centos6.8 修改yum安装镜像源

    查看centos系统版本 cat /etc/redhat-release CentOS系统更换软件安装源 第一步:备份你的原镜像文件,以免出错后可以恢复. mv /etc/yum.repos.d/Ce ...

  7. R中的参数传递函数:commandArgs(),getopt().

    1.commandArgs(),是R自带的参数传递函数,属于位置参数. ##test.R args=commandArgs(T) print (args[1])##第一个外部参数 print (arg ...

  8. Pandas标记删除重复记录

    Pandas提供了duplicated.Index.duplicated.drop_duplicates函数来标记及删除重复记录 duplicated函数用于标记Series中的值.DataFrame ...

  9. Pandas的 loc iloc ix 区别

    先看代码: In [46]: import pandas as pd In [47]: data = [[1,2,3],[4,5,6]] In [48]: index = [0,1] In [49]: ...

  10. Docker 搭建一个Docker应用栈

    Docker应用栈结构图 Build Django容器 编写docker-file FROM django RUN pip install redis build django-with-redis ...