I'm trying to represent a simple class heirarchy of:
Expense
-- LeaseExpense
-- TennantExpense
In this class heirarchy, the subclasses are used to determine which object the expense is applied to. To represent this I used the following code:
Code:
<class name="Expense" table="expenses">
...
<discriminator column="expenseType" type="string"/>
...
<subclass name="LeaseExpense" discriminator-value="lease">
<many-to-one name="appliedTo" column="lease" class="Lease"/>
</subclass>
<subclass name="TennantExpense" discriminator-value="tennant">
<many-to-one name="appliedTo" column="tennant" class="Tennant"/>
</subclass>
</class>
Everything looks straightforward, but when I create the DDL through hibernate I get the following interesting constraint:
Code:
alter table expenses
add constraint FK8CA4431B76C5718A
foreign key (tennant)
references tennants;
alter table expenses
add constraint FK8CA4431BD1F8DB8E
foreign key (lease)
references tennants;
You'll notice that the second constraint states that the "lease" column references the "tennants" table. This causes a constraint violation when I attempt to save a LeaseExpense.
If I rewrite this constraint by hand to reference the "leases" table then I can successfully save an Expense. But the fact that Hibernate is generating the wrong constraint is worrying me. It makes me think I'm doing something fundamentally wrong.
The questions I have are:
Am I doing something obviously wrong in the mapping?
Is there an easier way to do what I'm attempting?