views:

23

answers:

2

I have a domain model that looks like this:

     Instruction
       |      \
     Money    Other
     /   \ 
   Unit Cash 

and I want to map this model to my DB using JPA.

All classes map to the same table in the DB, (T_INSTRUCTION).

So I started out with jpa's SINGLE_TABLE inhertance strategy. Separating the Money and Other classes using the orm xml discriminate-column="MONEY_TYPE_ID".

My problem is I also want to sperate the sub classes Cash and Unit but they discriminate on a different column "CASH_TYPE_ID".

Scanning the docs this doesn't seem possible to me, but I was wondering does anyone out there have any tricks or workarounds to implement this?

Cheers

+1  A: 

A workaround may be to have different discriminator values for each subclass, and only one discriminator column. i.e. money=1, other=2, unit=3, cash=4

Bozho
Yeah that would work, but unfortunately the table is shared with other apps, so cant change its structure. cheers
alanl
+1  A: 

Having looked into this a little futher, there is no solution to this problem in the current JSR spec.

So what I decided to do in the end, was to use jpa single table inheritance for the 'Instruction' 'Money' and 'Other' types and then to allow 'Cash' and 'Unit' sub types of simple inhert from these. (i.e) 'discriminate-column="MONEY_TYPE_ID"'

Then use the constructor in the 'cash' and 'unit' to set field that maps to CASH_TYPE_ID.

PS: I'll post the code later

alanl