I read the reference and Patterns about inheritance in Hibernate, and I'd like to have some tip on my mapping. I translated my problem to the Payment example to make it clear.
I will start with the "happy programmer day" explained on the ref. book :
Case table per hierarchy, page 61, hibernate_reference.pdf (2.1.4)
Code:
<class name="Payment" table="PAYMENT">
<id name="id" type="long" column="PAYMENT_ID">
<generator class="native"/>
</id>
<discriminator column="PAYMENT_TYPE" type="string"/>
<property name="amount" column="AMOUNT"/>
...
<subclass name="CreditCardPayment" discriminator-value="CREDIT">
...
</subclass>
<subclass name="CashPayment" discriminator-value="CASH">
...
</subclass>
<subclass name="ChequePayment" discriminator-value="CHEQUE">
...
</subclass>
</class>
It uses the PAYMENT_TYPE column to discriminaete btw Credit,Cash,Cheque.
Now suppouse that PAYMENT_TYPE instead of being a simple column, maps to a metamodel
table, the class type is decided by a query like that
// Credit in old fashioned sql
Code:
Select * from Payment,PaymentType
where Payment.idType = PaymentType.idType
and PaymentType.idConfig=Payment.idConfig
and PaymentType.typeName="Credit"
(I have too another column like typeDesc with a descripion of Credit for the used configuration. I may call Credit with diferent names, but it is still Credit)
idConfig is a property present in the 2 tables that identifies the Payment model of my store. Some idconfigs will not have some PaymentTypes.
typeName is the-facto discriminator. But it's not part of Payment table. I can't use table per class to solve this.
I read the metadata solution posted by gavin in the blog
http://blog.hibernate.org/cgi-bin/blosxom.cgi/Gavin%20King/metadata-driven.htmlIt does not use inheritance to solve this issue, that ressembles my case in the dinamic classification of data.
This was the introduction. Here goes the true problem.
If I have a Sale Table that maps as follows:
Sale (1)-----(n) Payment
And a enforced trigger rule like
"A sale cannot have two payments of the same kind".
My natural mapping to class, with the assumption that the Set of PaymentTypes is finite and well known of Credit,Cheque and Cash is (like):
Code:
...class Sale {
public CashPayment getPaymentInChash() {
...
//may be null
}
public ChequePayment getPaymentInCheque() {
...
//may be null
}
public CreditPayment getPaymentInCredit() {
...
//may be null
}
// All the 3 cases in the same polymorphic collection
public Set getPayments() {
}
}
...abstract (?) class Payment {
// not null. I need getType to access description of paymentType
public PaymentType getType() {
...
}
public int ammount() {
}
}
...class CreditPayment {
public int creditCardNumber() {
...
}
}
The problem is how can I map this ? how can I turn
Sale (1) ---- (n) Payment
into
Sale (1) ---- (0-1) ChequePayment
Sale (1) ---- (0-1) CashPayment
Sale (1) ---- (0-1) CreditPayment
Currently I'm using a workaround like that:
Sale (1) ---- Set of Payment where="blablabla typeName='Cash'"
I'm taking advantage of the where clause of the set to separate in 3 sets. But it's not good.
I'm still reading about it in my (sparse) free time. I have spoted some ways that could solve this problem:
1. Custom Type
reading
http://www.hibernate.org/122.html
I discovered that it's possible to replace a column with an UserType. But , can I use it on the discriminator column ?? And I still will have the problem of 1-n
2. Lightweight class
http://www.hibernate.org/41.html
This cool design does not use inheritance. I could define class with the where clause (but not joined class or subclass!!!!). But I'm sure I will create a hell for myself, becouse without Payment super interface, I cannot use getPayments() polymorphic Set. Also I'm affraid about the evit() problem described by this pattern.
Am I pushing too far on the OR ?
it would make a terrific example to the book don't you think? ;)