7.5.1. 一对多(one to many) /多对一( many to one)

基于连接表的双向一对多关联。注意inverse="true"可以出现在关联的任意一端,即collection端或者join端。

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