7.3.2. 多对一(many 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" unique="true"/>
  8. <many-to-one name="address"
  9. column="addressId"
  10. not-null="true"/>
  11. </join>
  12. </class>
  13. <class name="Address">
  14. <id name="id" column="addressId">
  15. <generator class="native"/>
  16. </id>
  17. </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 )
  3. create table Address ( addressId bigint not null primary key )