Hibernate关联关系映射

本文详细介绍了ORM中关联关系的多种映射方式,包括单向和双向关联、使用及不使用连接表的情况,并通过具体的配置文件示例展示了如何实现这些关联。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值