DanielSerodio wrote:
Can you please elaborate on this? How do you join to another table once you've figure out which subclass you're using?
Inside the subclass' mapping, use the <join> mapping.
DanielSerodio wrote:
if I don't need to specify a discriminator value for each (joined) subclass, then how does Hibernate know which class to instantiate when reconstructing an object from the DB?
If a row in the superclass table has a corresponding row in the joined-subclass table "A", then it's an instance of class A. If instead it has a row in joined-subclass table "B", then it's an instance of class B.
Using the example in section 9.1.2 of the refdocs (section number may have changed, I have an old version of the refdocs):
Code:
<class name="Payment" table="PAYMENT">
<id name="id" type="long" column="PAYMENT_ID">
<generator class="native"/>
</id>
<property name="amount" column="AMOUNT"/>
...
<joined-subclass name="CreditCardPayment" table="CREDIT_PAYMENT">
<key column="PAYMENT_ID"/>
<property name="creditCardType" column="CCTYPE"/>
...
</joined-subclass>
<joined-subclass name="CashPayment" table="CASH_PAYMENT">
<key column="PAYMENT_ID"/>
...
</joined-subclass>
<joined-subclass name="ChequePayment" table="CHEQUE_PAYMENT">
<key column="PAYMENT_ID"/>
...
</joined-subclass>
</class>
Given an instance of the Payment superclass, with ID 1, hibernate can check the CREDIT_PAYMENT, CASH_PAYMENT and CHEQUE_PAYMENT tables. It finds value 1 in the PAYMENT_ID column of the CHEQUE_PAYMENT table, then it knows that Payment instance 1 is a ChequePayment.