`
easyworld
  • 浏览: 112362 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

Hibernate关联关系映射

阅读更多

Hibernate关联关系映射

关联关系映射的分类可以遵行这样一个原则:双向还是单项、是否有链接表, 依赖主键还是外键。
关联关系映射通常情况是最难配置正确的。在这个部分中,我们从单向关系映射开始,然后考虑双向关系映射,由浅至深讲述一遍典型的案例。在所有的例子中,我们都使用 Person和Address。

不使用连接表的单向关联:

1、单向many-to-one:
单向many-to-one关联是最常见的单向关联关系:
配置文件中配置多段,并在数据库表中添加 多端主键。
<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>
</class>
create table Person ( personId bigint not null primary key, addressId bigint not null )
create table Address ( addressId bigint not null primary key )


2、单向一对一(one to one):
基于外键关联的单向一对一关联和单向多对一关联几乎是一样的唯一的不同就是单向一对一关联中的外键字段具有唯一性约束。--标记为红色的,另外关于主键的生成方式 可以参考:http://easyworld.iteye.com/admin/blogs/214092

<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">

<!--hibernate 主键生产方式, -->
<generator class="native"/>
</id>
</class>
create table Person ( personId bigint not null primary key, addressId bigint not null unique )
create table Address ( addressId bigint not null primary key )

基于主键关联的单向一对一关联通常使用一个特定的id生成器,与基于外键的一对一格式差别在于数据库表的结构上。(请注意,在这个例子中我们掉换了关联的方向。)

<class name="Person">

<id name="id" column="personId">

<generator class="native"/>

</id>

</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 )

基于主键关联的单向一对一关联通常使用一个特定的id生成器。(请注意,在这个例子中我们掉换了关联的方向。)

<class name="Person">

<id name="id" column="personId">

<generator class="native"/>

</id>

</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 )

使用连接表的单向关联(Unidirectional associations with join tables)

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

<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"

unique="true"

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 not null, addressId bigint not null primary key )

create table Address ( addressId bigint not null primary key )

2、基于连接表的单向多对一关联在关联关系可选的情况下应用也很普遍。链接表的主键为多端主键。

<class name="Person">

<id name="id" column="personId">

<generator class="native"/>

</id>

<join table="PersonAddress"

optional="true">

<key column="personId" unique="true"/>

<many-to-one name="address"

column="addressId"

not-null="true"/>

</join>

</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 primary key, addressId bigint not null )

create table Address ( addressId bigint not null primary key )



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

<class name="Person">

<id name="id" column="personId">

<generator class="native"/>

</id>

<join table="PersonAddress"

optional="true">

<key column="personId"

unique="true"/>

<many-to-one name="address"

column="addressId"

not-null="true"

unique="true"/>

</join>

</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 primary key, addressId bigint not null unique )

create table Address ( addressId bigint not null primary key )



4、多对多

最后,还有 单向多对多关联. 主要是 在链接表中添加了联合主键

<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 )

 

分享到:
评论

相关推荐

    hibernate关联关系映射

    "hibernate关联关系映射"是Hibernate的核心概念之一,它定义了如何在Java对象和数据库表之间建立关系。以下是对这个主题的详细讲解: 一、一对一关系映射(One-to-One) 一对一关系是指一个实体对应另一个实体的...

    Hibernate 关联关系映射分类

    在深入探讨Hibernate关联关系映射分类之前,我们首先简要回顾一下Hibernate框架的基本概念。Hibernate是一种持久层框架,主要用于Java应用程序中的对象关系映射(ORM),它能够将面向对象的数据模型转换为数据库中的...

    Hibernate关联关系映射目录

    ### Hibernate关联关系映射 #### 一、单向关联 单向关联指的是对象之间的关联关系只在一个方向上存在,也就是说这种关联关系仅在一个类中表示出来,在另一个类中不体现这种关联。 ##### 1. 一对一外键单向关联 ...

    Hibernate关联关系映射.CHM

    Hibernate关联关系映射.CHM Hibernate文档相关

    Hibernate关联关系映射实例速查

    Hibernate关联关系映射实例速查,帮助初学者学习。

    Hibernate的关联关系映射图解

    Hibernate4中映射关系图解。

    hibernate关联关系映射PPT

    hibernate中的关联关系映射ppt!觉得有用的可以下!

    hibernate关联映射详解

    hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,包含4个说明文档,分别详细解说了hibernate关联映射的...

    hibernate的关联关系映射

    【hibernate的关联关系映射】在Java持久化框架Hibernate中,关联关系映射是核心功能之一,它允许对象间的复杂关系与数据库中的表结构相匹配。在选课系统这个例子中,主要涉及到的对象包括课题(Course)、教师(Teacher...

    Hibernate_关联关系映射配置详解

    Hibernate_关联关系映射配置详解,希望能帮助广大java爱好者

    hibernate关联映射实例

    本文将深入探讨“hibernate关联映射实例”中的关键知识点,包括一对多、多对多和继承映射,这些都是Hibernate中至关重要的概念。 1. **一对多关联映射**: 在现实世界中,一个实体可能会与多个其他实体相关联,...

    Hibernate_关联关系映射配置

    本教程将详细讲解Hibernate中的关联关系映射配置,帮助你理解和掌握如何在Hibernate中设置不同类型的关联。 一、一对一(One-to-One)关联 在现实世界中,两个实体之间可能存在一对一的关系,例如一个人只有一个...

    Hibernate_实体关联关系映射--学习总结

    Hibernate的实体关联映射是其核心功能之一,它通过灵活的配置来适应不同场景下的数据持久化需求。通过对一对一、一对多、多对多等映射关系的熟练掌握和灵活运用,可以有效地处理复杂的实体关系,提高开发效率和应用...

    hibernate关联映射的作用和常用属性解释

    ### Hibernate关联映射的作用与常用属性详解 #### 关联映射概述 在对象关系映射(Object Relational Mapping,简称ORM)技术中,Hibernate作为Java领域内非常成熟且功能强大的框架之一,它允许开发者将Java类映射...

    Hibernate注解 关联关系映射注解.docx

    在Java的持久化框架Hibernate中,注解是用于简化对象关系映射(ORM)的一种方式。本篇文章将详细探讨在Hibernate中如何使用注解来处理各种关联关系,特别是`mappedBy`属性的用法。 首先,`@OneToMany`注解用于表示...

Global site tag (gtag.js) - Google Analytics