一、单向关联(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

最新文章

  1. 《Javascript设计模式》笔记一js的表现力
  2. 火狐插件 Http请求利器 Httprequester
  3. centos6搭建VPN
  4. ACMer(转)
  5. Transact-SQL
  6. 【Android - MD】之Snackbar的使用
  7. HDU 1848 Fibonacci again and again
  8. [TroubleShooting]&amp;#39;trn\bak&amp;#39; is incorrectly formed. SQL Server cannot process this media family.
  9. NetBeans工具学习之道:NetBeans IDE Java 高速新手教程
  10. CentOS 7 安装 JDK
  11. iOS之内存分析
  12. 线性代数-矩阵-【1】矩阵汇总 C和C++的实现
  13. F - Capture
  14. 什么是Vagrant
  15. DOS批处理高级教程(还不错)(转)
  16. js datagrid 移动去重
  17. C语言实现字符串逆序输出
  18. ubuntu 安装source insight3.5
  19. C# 多线程下 静态类字段异常
  20. laravel学习历程

热门文章

  1. Java基础—数据类型
  2. make menuconfig 时出现 mixed implicit and normal rules: deprecated syntax
  3. BIO,NIO和AIO
  4. Android系统源代码的下载与编译
  5. linux du与ls查看文件大小时的区别
  6. Model FEP 快易播看板推播系统
  7. 使用bedtools的一个问题
  8. INSPIRED启示录 读书笔记 - 第25章 快速响应阶段
  9. INSPIRED启示录 读书笔记 - 第7章 管理产品经理
  10. JSP的动态Include的静态Include