9.1.2. 每个子类一张表(Table per subclass)

对于上例中的几个类而言,采用“每个子类一张表”的映射策略,代码如下所示:

  1. <class name="Payment" table="PAYMENT">
  2. <id name="id" type="long" column="PAYMENT_ID">
  3. <generator class="native"/>
  4. </id>
  5. <property name="amount" column="AMOUNT"/>
  6. ...
  7. <joined-subclass name="CreditCardPayment" table="CREDIT_PAYMENT">
  8. <key column="PAYMENT_ID"/>
  9. ...
  10. </joined-subclass>
  11. <joined-subclass name="CashPayment" table="CASH_PAYMENT">
  12. <key column="PAYMENT_ID"/>
  13. <property name="creditCardType" column="CCTYPE"/>
  14. ...
  15. </joined-subclass>
  16. <joined-subclass name="ChequePayment" table="CHEQUE_PAYMENT">
  17. <key column="PAYMENT_ID"/>
  18. ...
  19. </joined-subclass>
  20. </class>

需要四张表。三个子类表通过主键关联到超类表(因而关系模型实际上是一对一关联)。