首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

hibernate联系关系映射

2012-09-03 
hibernate关联映射单向关联 多对一(many to one)单向many-to-one关联是最常见的单向关联关系。class name

hibernate关联映射
单向关联
多对一(many to one)
单向many-to-one关联是最常见的单向关联关系。

<class name="Person">
    <id name="id" column="personId">
        <generator
        column="addressId"
        not-null="true"/>
</class>
<class name="Address">
    <id name="id" column="addressId">
        <generator column="personId">
        <generator
        column="addressId"
        unique="true"
        not-null="true"/>
</class>
<class name="Address">
    <id name="id" column="addressId">
        <generator column="personId">
        <generator column="personId">
        <generator constrained="true"/>
</class>
create table Person ( personId bigint not null primary key )
create table Address ( personId bigint not null primary key )
       
一对多(one to many)
基于外键关联的单向一对多关联是一种很少见的情况,并不推荐使用。

<class name="Person">
    <id name="id" column="personId">
        <generator
            not-null="true"/>
        <one-to-many column="addressId">
        <generator column="personId">
        <generator table="PersonAddress">
        <key column="personId"/>
        <many-to-many column="addressId"
            unique="true"
            column="addressId">
        <generator column="personId">
        <generator
        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 column="personId">
        <generator
        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 column="personId">
        <generator table="PersonAddress">
        <key column="personId"/>
        <many-to-many column="addressId"
            column="addressId">
        <generator column="personId">
        <generator
        column="addressId"
        not-null="true"/>
</class>
<class name="Address">
    <id name="id" column="addressId">
        <generator inverse="true">
        <key column="addressId"/>
        <one-to-many and insert="false"来对另一端反向操作)。

<class name="Person">
   <id name="id"/>
   ...
   <many-to-one name="address"
      column="addressId"
      not-null="true"
      insert="false"
      update="false"/>
</class>
<class name="Address">
   <id name="id"/>
   ...
   <list name="people">
      <key column="addressId" not-null="true"/>
      <list-index column="peopleIdx"/>
      <one-to-many column="personId">
        <generator
        column="addressId"
        unique="true"
        not-null="true"/>
</class>
<class name="Address">
    <id name="id" column="addressId">
        <generator
        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 )
       
基于主键关联的一对一关联需要使用特定的id生成器。

<class name="Person">
    <id name="id" column="personId">
        <generator column="personId">
        <generator
        constrained="true"/>
</class>
create table Person ( personId bigint not null primary key )
create table Address ( personId bigint not null primary key )
       
使用连接表的双向关联
一对多(one to many) /多对一( many to one)
基于连接表的双向一对多关联。注意inverse="true"可以出现在关联的任意一端,即collection端或者join端。

<class name="Person">
    <id name="id" column="personId">
        <generator
        table="PersonAddress">
        <key column="personId"/>
        <many-to-many column="addressId"
            unique="true"
            column="addressId">
        <generator
        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 )
       
一对一(one to one)
基于连接表的双向一对一关联极为罕见,但也是可行的。

<class name="Person">
    <id name="id" column="personId">
        <generator
        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
        optional="true"
        inverse="true">
        <key column="addressId"
            unique="true"/>
        <many-to-one name="person"
            column="personId"
            not-null="true"
            unique="true"/>
    </join>
</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 column="addressId">
        <generator 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 )

热点排行