7.3.3. 一对一(one to one)

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

  1. <class name="Person">
  2. <id name="id" column="personId">
  3. <generator class="native"/>
  4. </id>
  5. <join table="PersonAddress"
  6. optional="true">
  7. <key column="personId"
  8. unique="true"/>
  9. <many-to-one name="address"
  10. column="addressId"
  11. not-null="true"
  12. unique="true"/>
  13. </join>
  14. </class>
  15. <class name="Address">
  16. <id name="id" column="addressId">
  17. <generator class="native"/>
  18. </id>
  19. </class>
  1. create table Person ( personId bigint not null primary key )
  2. create table PersonAddress ( personId bigint not null primary key, addressId bigint not null unique )
  3. create table Address ( addressId bigint not null primary key )