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

来源:互联网 发布:监控组态软件 编辑:程序博客网 时间:2024/05/21 10:21

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 )

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

------------------------------------------

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

该情况不再举例说明。

0 0