I'd greatly appreciate mapping advice on this; despite re-readings of the Hibernate literature and forums, I'm not grokking this one. It's very simple, but apparently so am I.
I have a Loan class with properties of type PrimaryApplicant and CoApplicant. Both PrimaryApplicant and CoApplicant extend Applicant. (primaryApplicant is required, coApplicant is optional.)
Now the question: how do I use joined-subclass (my preference for subclass mappings) so that my applicants are all in the APPLICANT table, with its own primary key, yet my LOAN table references PRIMARY_APPLICANT and CO_APPLICANT properly (or vice versa)?
I've tried various combinations, and I either get LOAN to properly reference PRIMARY and CO via proper foreign keys, or I get PRIMARY and CO to properly reference APPLICANT via APPLICANT_ID. But I can't get those at the same time!
I want to make sure that when I delete or update a loan, all related APPLICANT/CO/PRIMARY is cleaned up properly.
Here's my attempted mapping, which I know is wrong.
Code:
<class name="Loan" table="LOAN">
<id name="loanId" type="string">
<column name="LOAN_ID" length="10"/>
<generator class="assigned"/>
</id>
<one-to-one name="primaryApplicant" class="PrimaryApplicant"/>
<one-to-one name="coApplicant" class="CoApplicant"/>
...
</class>
<class name="Applicant" table="APPLICANT">
<id name="applicantId" column="APPLICANT_ID" type="integer" unsaved-value="-1">
<generator class="sequence">
<param name="sequence">APPLICANT_ID_SEQ</param>
</generator>
</id>
...
</class>
<joined-subclass name="PrimaryApplicant" extends="Applicant" table="PRIMARY_APPLICANT">
<key column="APPLICANT_ID"/>
...
</joined-subclass>
<joined-subclass name="CoApplicant" extends="Applicant" table="CO_APPLICANT">
<key column="APPLICANT_ID"/>
...
</joined-subclass>
Here's the table structure I'd like to see - my goals is to keep APPLICANT "free" of loan references, although that's not critical. It could be used for non-loan purposes, so my intention was to put references to LOAN only in CO and PRIMARY.
Code:
TABLE LOAN (
LOAN_ID PRIMARY_KEY
CO_APP_ID references CO_APPLICANT
PRIMARY_APP_ID references PRIMARY_APPLICANT
...
)
TABLE APPLICANT (
APPLICANT_ID PRIMARY_KEY
...
)
TABLE PRIMARY_APPLICANT (
LOAN_ID PRIMARY_KEY references LOAN
APPLICANT_ID references APPLICANT
...
)
TABLE CO_APPLICANT (
LOAN_ID PRIMARY_KEY references LOAN
APPLICANT_ID references APPLICANT
...
)