您的位置:首页 > 其它

Hibernate映射关系解析(三)--Unidirectional associations--one-to-many

2015-10-26 23:53 721 查看
8.2.3 一对多(one-to-many)

基于外键的单向一对多(one-to-many))关联不是一种通用的做法(是一种很少见的做法),并不推荐使用。

<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<set name="addresses">
<key column="personId"
not-null="true"/>
<one-to-many 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 Address ( addressId bigint not null primary key, personId bigint not null )


对于这种关联关系最好使用连接表。

------------------------------------------
我认为,基于外键的一对多的关联,这样主表中除外键外,有很多重复记录 ,这样势必造成主表是没有主键的(主键不允许重复),不符合关系型数据库的设计思想。

该情况不再举例说明。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: