one2one

1、基于外键关联的单向一对一关联
   和单向多对一关联几乎是一样的。唯一的不同就是单向一对一关联中的外键字段具有唯一性约束。

 <class name="com.jason.associations.one2one.unilateralism.foreign.Person" table="Person">
  <id name="id" column="personId">
   <generator class="native"/>
  </id>
  <many-to-one name="address"
   class="com.jason.associations.one2one.unilateralism.foreign.Address"
   column="addressId"
      unique="true"
      cascade="all"
      not-null="true"/>
 </class>
 
 <class name="com.jason.associations.one2one.unilateralism.foreign.Address" table="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)
 -->


2、基于主键关联的单向一对一关联
   通常使用一个特定的id生成器。(请注意,在这个例子中我们掉换了关联的方向。)

 <class name="com.jason.associations.one2one.unilateralism.primary.Person" table="Person">
  <id name="id" column="personId">
   <generator class="native"/>
  </id>
 </class>
 
 <class name="com.jason.associations.one2one.unilateralism.primary.Address" table="Address">
  <id name="id" column="addressId">
      <generator class="foreign">
    <param name="property">person</param>
   </generator>
  </id>
  <one-to-one name="person"
   class="com.jason.associations.one2one.unilateralism.primary.Person"
   constrained="true"/>
 </class>
 <!--
  create table Person ( personId bigint not null primary key)
  create table Address ( addressId bigint not null primary key)  
  其中personId= addressId
 -->


3、基于连接表的单向一对一关联非常少见,但也是可行的。

 <class name="com.jason.associations.one2one.unilateralism.table.Person" table="Person">
  <id name="id" column="personId">
   <generator class="native"/>
  </id>
      <!--join 必须放在最下面-->
       <join table="PersonAddress" optional="true">
           <key column="personId" unique="true"/>
           <many-to-one name="address"
               column="addressId"
               not-null="true"
            cascade="all"
               unique="true"/>
        </join> 
 </class>
 
 <class name="com.jason.associations.one2one.unilateralism.table.Address" table="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)
 -->


4、对应的双向关联
a、单向关联和双向关联使用的表是一样的
b、双向关联类中多了属性
c、双向关联可以从任何一个对象开始操作(比如添加),而单向只能从特定的一方操作