This is an example Table Per Class Hierarchy mapping:
Payment - Base (SuperType)
Code:
@Entity
@Table(name = "PAYMENT")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn( name = "type", discriminatorType = DiscriminatorType.STRING)
@DiscriminatorValue("BASE")
public class Payment {
private Product product;
private Date date;
private Customer Customer;
getters/setters
}
CreditCard
Code:
@Entity
@DiscriminatorValue("CC")
public class CreditCard extends Payment {
private String Account
getters/setters
}
Cash
Code:
@DiscriminatorValue("CASH")
public class Cash extends Payment {
private String Paper
private String Coins
getters/setters
}
Hibernate is able to accept an instancce of Payment, or Credit or Cash referenced as the Payment type and persist the correct instance thru some really complex code I traced through. What an exercise that was. :-)
My question is centered around working with the Payment instance polymorhically in my code . I would like to reference Payment anytime but each subclass adds unique instance fields so i lose those methods when working with a Payment reference. I could stub out these subclass fields on Payment itself to gain the benefit of polymorphism, but I don't think this is the correct way. I can also add a bunch of if/thens to create concrete instances and then work with those, but again does not seem like the right way to do things. I suspect either that my mapping is incorrect or I need to re-work my code. The table per class hierarchy mapping seems very clean and logical and would like to keep it, but not at the cost of fighting it elsewhere.
Thanks
-J