7.5.2. 一对一(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. <join table="PersonAddress"
  20. optional="true"
  21. inverse="true">
  22. <key column="addressId"
  23. unique="true"/>
  24. <many-to-one name="person"
  25. column="personId"
  26. not-null="true"
  27. unique="true"/>
  28. </join>
  29. </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 )