码农小汪-Hibernate学习4-关联关系映射many-to-one,many-to-one,one-to-many

关系关系,复杂!单向关联,双向关联,了解非常的必要,而且要熟练的掌握才是很有必要的,我们下面采用XML的方式使用

单向关联(Unidirectional associations)

单向 many-to-one 关联是最常见的单向关联关系

多个人住在同一个地址,我们只需要关注这个人的地址,没有必要关注这个地址住了多少人,所以这个是单向关联的

<class name="Person">
   <id name="id" column="personId">
         <generator class="native"/>
   </id>
    <many-to-one name="address"
          column="addressId" 数据表中的表项,其实就是Address中的主键的值
          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 )
只要我们一看数据表就知道什么问题了,什么many-to-one你知道了吧!

一对一(One-to-one)

基于外键关联的单向一对一关联和单向多对一关联几乎是一样的。唯一的不同就是单向一对一关联中的外键字段具有唯一性约束。我们每人人的地址不能是一样的哦,不能使用同一个外键 地址这个对象的ID,只能一个

<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 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 unique )
create table Address ( addressId bigint not null primary key )

这个和我们的Manytoone都差不多

一对多(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 )
我们认为对于这种关联关系最好使用连接表。

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

一对多(one-to-many) 连接表的意思就是我们产出一个关系表项
基于连接表的单向一对多关联 应该优先被采用。请注意,通过指定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 )

多对一(many-to-one)
基于连接表的单向多对一关联在关联关系可选的情况下应用也很普遍。例如:

<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

一对一(One-to-one)
基于连接表的单向一对一关联也是可行的,但非常少见

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

多对多(many-to-many)
最后,这里是一个单向多对多关联的例子。

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

双向关联(Bidirectional associations)

一对多(one to many)/多对一(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>
    <set name="people" inverse="true">
        <key column="addressId"/>
        <one-to-many class="Person"/>
    </set>
</class
>
看inverse=true 我们的Address表中没得person Id的数据
create table Person ( personId bigint not null primary key, addressId bigint not null )
create table Address ( addressId bigint not null primary key )

一对一(One-to-one)

<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">
        <generator class="native"/>
    </id>
    <one-to-one name="person"
       property-ref="address"/>
</class
>

create table Person ( personId bigint not null primary key, addressId bigint not null unique )
create table Address ( addressId bigint not null primary key )

使用连接表的双向关联(Bidirectional associations with join tables)

一对多(one to many)/多对一(many to one)
下面是一个基于连接表的双向一对多关联的例子。注意 inverse=”true” 可以出现在关联的任意一端,即 collection 端或者 join 端。

<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>
    <join table="PersonAddress"
        inverse="true"
        optional="true">
        <key column="addressId"/>
            <many-to-one name="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 bigint not null primary key )
create table Address ( addressId bigint not null primary key )

多对多(many-to-many)
下面是一个双向多对多关联的例子

<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>
    <set name="people" inverse="true"     table="PersonAddress">
        <key column="addressId"/>
        <many-to-many column="personId"
        class="Person"/>
    </set>
</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)