第8章关联关系映射
8.1.介绍
关联关系映射通常情况是最难配置正确的。在这个部分中,我们从单向关系映射开始,然后考虑双向关系映射,由浅至深讲述一遍典型的案例。在所有的例子中,我们都使用Person和Address。
我们根据映射关系是否涉及连接表以及多样性来划分关联类型。
在传统的数据建模中,允许为Null值的外键被认为是一种不好的实践,因此我们所有的例子中都使用不允许为Null的外键。这并不是Hibernate的要求,即使你删除掉不允许为Null的约束,Hibernate映射一样可以工作的很好。
8.2.单向关联(Unidirectional associations)
8.2.1.多对一(many to one)
单向many-to-one关联是最常见的单向关联关系。
<class name="Person">
<id name="id"column="personId">
<generatorclass="native"/>
</id>
<many-to-onename="address"
column="addressId"
not-null="true"/>
</class>
<class name="Address">
<id name="id"column="addressId">
<generator class="native"/>
</id>
</class>
create table Person ( personId bigint not null primary key, addressIdbigint not null )
create table Address ( addressId bigint not null primary key )
8.2.2.一对一(one to one)
基于外键关联的单向一对一关联和单向多对一关联几乎是一样的。唯一的不同就是单向一对一关联中的外键字段具有唯一性约束。
<class name="Person">
<id name="id"column="personId">
<generatorclass="native"/>
</id>
<many-to-onename="address"
column="addressId"
unique="true"
not-null="true"/>
</class>
<class name="Address">
<id name="id"column="addressId">
<generatorclass="native"/>
</id>
</class>
create table Person ( personId bigint not null primary key, addressIdbigint not null unique )
create table Address ( addressId bigint not null primary key )
基于主键关联的单向一对一关联通常使用一个特定的id生成器。(请注意,在这个例子中我们掉换了关联的方向。)
<class name="Person">
<id name="id"column="personId">
<generatorclass="native"/>
</id>
</class>
<class name="Address">
<id name="id"column="personId">
<generatorclass="foreign">
<paramname="property">person</param>
</generator>
</id>
<one-to-onename="person" constrained="true"/>
</class>
create table Person ( personId bigint not null primary key )
create table Address ( personId bigint not null primary key )
8.2.3.一对多(one to many)
基于外键关联的单向一对多关联是一种很少见的情况,并不推荐使用。
<class name="Person">
<id name="id"column="personId">
<generatorclass="native"/>
</id>
<setname="addresses">
<keycolumn="personId"
not-null="true"/>
<one-to-manyclass="Address"/>
</set>
</class>
<class name="Address">
<id name="id"column="addressId">
<generatorclass="native"/>
</id>
</class>
create table Person ( personId bigint not null primary key )
create table Address ( addressId bigint not null primary key, personIdbigint not null )
我们认为对于这种关联关系最好使用连接表。
8.3.使用连接表的单向关联(Unidirectional associationswith join tables)
8.3.1.一对多(oneto many)
基于连接表的单向一对多关联 应该优先被采用。请注意,通过指定unique="true",我们可以把多样性从多对多改变为一对多。
<class name="Person">
<id name="id"column="personId">
<generatorclass="native"/>
</id>
<setname="addresses" table="PersonAddress">
<keycolumn="personId"/>
<many-to-manycolumn="addressId"
unique="true"
class="Address"/>
</set>
</class>
<class name="Address">
<id name="id"column="addressId">
<generatorclass="native"/>
</id>
</class>
create table Person ( personId bigint not null primary key )
create table PersonAddress ( personId not null, addressId bigint not nullprimary key )
create table Address ( addressId bigint not null primary key )
8.3.2.多对一(manyto one)
基于连接表的单向多对一关联在关联关系可选的情况下应用也很普遍。
<class name="Person">
<id name="id"column="personId">
<generatorclass="native"/>
</id>
<jointable="PersonAddress"
optional="true">
<keycolumn="personId" unique="true"/>
<many-to-onename="address"
column="addressId"
not-null="true"/>
</join>
</class>
<class name="Address">
<id name="id"column="addressId">
<generatorclass="native"/>
</id>
</class>
create table Person ( personId bigint not null primary key )
create table PersonAddress ( personId bigint not null primary key,addressId bigint not null )
create table Address ( addressId bigint not null primary key )
8.3.3.一对一(oneto one)
基于连接表的单向一对一关联非常少见,但也是可行的。
<class name="Person">
<id name="id"column="personId">
<generatorclass="native"/>
</id>
<jointable="PersonAddress"
optional="true">
<keycolumn="personId"
unique="true"/>
<many-to-onename="address"
column="addressId"
not-null="true"
unique="true"/>
</join>
</class>
<class name="Address">
<id name="id"column="addressId">
<generatorclass="native"/>
</id>
</class>
create table Person ( personId bigint not null primary key )
create table PersonAddress ( personId bigint not null primary key,addressId bigint not null unique )
create table Address ( addressId bigint not null primary key )
8.3.4.多对多(manyto many)
最后,还有 单向多对多关联.
<class name="Person">
<id name="id"column="personId">
<generatorclass="native"/>
</id>
<setname="addresses" table="PersonAddress">
<keycolumn="personId"/>
<many-to-manycolumn="addressId"
class="Address"/>
</set>
</class>
<class name="Address">
<id name="id"column="addressId">
<generatorclass="native"/>
</id>
</class>
create table Person ( personId bigint not null primary key )
create table PersonAddress ( personId bigint not null, addressId bigintnot null, primary key (personId, addressId) )
create table Address ( addressId bigint not null primary key )
8.4.双向关联(Bidirectional associations)
8.4.1.一对多(one to many) / 多对一(many to one)
双向多对一关联 是最常见的关联关系。(这也是标准的父/子关联关系。)
<class name="Person">
<id name="id"column="personId">
<generatorclass="native"/>
</id>
<many-to-onename="address"
column="addressId"
not-null="true"/>
</class>
<class name="Address">
<id name="id"column="addressId">
<generatorclass="native"/>
</id>
<set name="people"inverse="true">
<key column="addressId"/>
<one-to-manyclass="Person"/>
</set>
</class>
create table Person ( personId bigint not null primary key, addressIdbigint not null )
create table Address ( addressId bigint not null primary key )
8.4.2.一对一(one to one)
基于外键关联的双向一对一关联也很常见。
<class name="Person">
<id name="id"column="personId">
<generatorclass="native"/>
</id>
<many-to-onename="address"
column="addressId"
unique="true"
not-null="true"/>
</class>
<class name="Address">
<id name="id"column="addressId">
<generatorclass="native"/>
</id>
<one-to-onename="person"
property-ref="address"/>
</class>
create table Person ( personId bigint not null primary key, addressIdbigint not null unique )
create table Address ( addressId bigint not null primary key )
基于主键关联的一对一关联需要使用特定的id生成器。
<class name="Person">
<id name="id"column="personId">
<generatorclass="native"/>
</id>
<one-to-onename="address"/>
</class>
<class name="Address">
<id name="id"column="personId">
<generatorclass="foreign">
<paramname="property">person</param>
</generator>
</id>
<one-to-onename="person"
constrained="true"/>
</class>
create table Person ( personId bigint not null primary key )
create table Address ( personId bigint not null primary key )
8.5.使用连接表的双向关联(Bidirectional associationswith join tables)
8.5.1.一对多(oneto many) /多对一( many to one)
基于连接表的双向一对多关联。注意inverse="true"可以出现在关联的任意一端,即collection端或者join端。
<class name="Person">
<id name="id"column="personId">
<generatorclass="native"/>
</id>
<setname="addresses"
table="PersonAddress">
<keycolumn="personId"/>
<many-to-manycolumn="addressId"
unique="true"
class="Address"/>
</set>
</class>
<class name="Address">
<id name="id"column="addressId">
<generatorclass="native"/>
</id>
<jointable="PersonAddress"
inverse="true"
optional="true">
<keycolumn="addressId"/>
<many-to-onename="person"
column="personId"
not-null="true"/>
</join>
</class>
create table Person ( personId bigint not null primary key )
create table PersonAddress ( personId bigint not null, addressId bigintnot null primary key )
create table Address ( addressId bigint not null primary key )
8.5.2.一对一(oneto one)
基于连接表的双向一对一关联极为罕见,但也是可行的。
<class name="Person">
<id name="id"column="personId">
<generatorclass="native"/>
</id>
<jointable="PersonAddress"
optional="true">
<keycolumn="personId"
unique="true"/>
<many-to-onename="address"
column="addressId"
not-null="true"
unique="true"/>
</join>
</class>
<class name="Address">
<id name="id"column="addressId">
<generatorclass="native"/>
</id>
<jointable="PersonAddress"
optional="true"
inverse="true">
<keycolumn="addressId"
unique="true"/>
<many-to-onename="address"
column="personId"
not-null="true"
unique="true"/>
</join>
</class>
create table Person ( personId bigint not null primary key )
create table PersonAddress ( personId bigint not null primary key,addressId bigint not null unique )
create table Address ( addressId bigint not null primary key )
8.5.3.多对多(manyto many)
最后,还有 双向多对多关联.
<class name="Person">
<id name="id"column="personId">
<generatorclass="native"/>
</id>
<setname="addresses">
<keycolumn="personId"/>
<many-to-manycolumn="addressId"
class="Address"/>
</set>
</class>
<class name="Address">
<id name="id"column="addressId">
<generatorclass="native"/>
</id>
<set name="people"inverse="true">
<key column="addressId"/>
<many-to-manycolumn="personId"
class="Person"/>
</set>
</class>
create table Person ( personId bigint not null primary key )
create table PersonAddress ( personId bigint not null, addressId bigintnot null, primary key (personId, addressId) )
create table Address ( addressId bigint not null primary key )
|
|
|
第7章集合类(Collections)映射
|
起始页
|
第9章组件(Component)映射
|
来自:http://docs.huihoo.com/hibernate/reference-v3_zh-cn/associations.html
分享到:
相关推荐
Hibernate.jar包,Hibernate可以应用在任何使用JDBC的场合,包含 hibernate-commons-annotations-4.0.1.Final.jar hibernate-core-4.1.12.Final.jar hibernate-ehcache-4.1.12.Final.jar hibernate-entitymanager-...
"Hibernate入门到精通" Hibernate 是一个基于Java的ORM(Object-Relational Mapping,对象关系映射)框架,它提供了一种简洁高效的方式来访问和操作关系数据库。下面是 Hibernate 的主要知识点: Hibernate 简介 ...
hibernate-commons-annotations-4.0.1.Final.jar hibernate-core-4.1.12.Final.jar hibernate-ehcache-4.1.12.Final.jar hibernate-entitymanager-4.1.12.Final.jar hibernate-jpa-2.0-api-1.0.1.Final.jar ...
项目原型:Struts2.3.16 + Spring4.1.1 + Hibernate4.3.6 二、 项目目的: 整合使用最新版本的三大框架(即Struts2、Spring4和Hibernate4),搭建项目架构原型。 项目架构原型:Struts2.3.16 + Spring4.1.1 + ...
【hibernate学习资料大全】 Hibernate 是一个开源的对象关系映射(ORM)框架,它极大地简化了Java应用程序对数据库的操作。这个压缩包包含了丰富的Hibernate学习资源,旨在帮助开发者深入理解和熟练掌握这一强大的...
《Hibernate-Extensions全面指南》 Hibernate,作为Java领域中的一款著名对象关系映射(ORM)框架,极大地简化了数据库操作。然而,为了满足更复杂的业务需求,Hibernate还提供了丰富的扩展功能,这就是我们今天要...
【描述】中的"hibernate的jar包"指的是Hibernate框架的运行库文件,这些JAR文件包含了Hibernate的所有核心API、实现和依赖库,如Hibernate Commons Annotations、Hibernate EntityManager、Hibernate Core等。...
Hibernate是一个开源的对象关系映射(ORM)框架,它允许Java开发者使用面向对象的方式来操作数据库,极大地简化了数据访问层的编程工作。这个压缩包包含了Hibernate的基础jar包,这些jar文件是开发Hibernate应用所...
HibernateTools是Java开发人员在使用Hibernate ORM框架时的有力辅助工具集,主要目的是为了提高开发效率,简化数据库操作。在HibernateTools 3.2.4版本中,它包含了一系列的特性与插件,以支持更便捷地进行对象关系...
Hibernate Spatial 4 教程 Hibernate Spatial 是一个基于 Hibernate 或 JPA 的空间几何对象数据库操作框架,提供了实现空间几何对象数据库操作的方法和配置说明。 一、 Hibernate Spatial 4 简介 Hibernate ...
Hibernate3 是一个强大的Java持久化框架,它允许开发者将数据库操作与业务逻辑解耦,使得应用程序的开发更为简便。这个“hibernate3全部jar包:hibernate3.jar.zip”包含了所有必要的库文件,方便用户一次性下载并...
Hibernate3是一个广泛使用的Java对象关系映射(ORM)框架,它允许开发者用面向对象的方式处理数据库操作,极大地简化了Java应用程序与数据库之间的交互。在这个"Hibernate3的依赖包"中,包含了运行Hibernate3应用...
在Java开发环境中,与KingbaseV8数据库进行交互通常会用到Hibernate框架和JDBC驱动。 Hibernate是一个优秀的对象关系映射(ORM)框架,它简化了Java应用程序对数据库的操作,通过将Java对象与数据库表进行映射,...
标题中的“hibernate和MySQL的jar”指的是Hibernate ORM框架与MySQL数据库之间的连接库。Hibernate是一种流行的Java对象关系映射(ORM)工具,它允许开发者使用面向对象的编程方式来操作数据库,而无需直接编写SQL...
标题"Hibernate 中文api 等学习资料"暗示了这是一组针对Hibernate ORM框架的中文学习资源,包括API文档和其他指南,旨在帮助用户更好地理解和使用Hibernate。 描述中的"hibernate orm框架api中文文档,学习资料,...
包含hibernate所有所需jar包还有一些其他包日志包、jpa支持包等: 列如:hibernate-core-5.1.0.Final.jar hibernate-ehcache-5.1.0.Final.jar hibernate-entitymanager-5.1.0.Final.jar hibernate-envers-5.1.0....
### Hibernate2 升级至 Hibernate3 的注意事项 #### 背景与目的 随着技术的发展,软件框架也在不断地更新迭代,以适应更多的需求并优化性能。Hibernate 作为一款流行的 Java 持久层框架,其从版本 2 升级到版本 3...
Hibernate是一款强大的Java持久化框架,它简化了数据库与Java对象之间的交互,使开发者可以更加专注于业务逻辑而不是数据访问层的实现。本压缩包提供的是Hibernate入门所需的jar包,包括了Hibernate的核心库以及与之...
Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库。 Hibernate可以应用在任何使用JDBC的场合,既可以在Java的客户端...
Hibernate3 是一个非常重要的Java持久化框架,它允许开发者将对象模型与数据库关系模型进行映射,从而简化了数据存取的过程。这个压缩包“hibernate3必要jar包”显然是针对Hibernate3版本的,已经去除了冗余的库文件...