Here is my question –
There are two classes Order and PaymentType. An order will have one PaymentType. Now PaymentType is an abstract class and there are other two classes called CreditCardPaymentType and PaypalPaymentType which extends PaymentType class. In hibernate they are modeled as Single_Table strategy. CreditCardPaymentType contains a field called CreditCardNumber. I am writing following HQL and my intention is to eagerly load the CreditCardNumber entity.
session.createQuery("select order from Order as order " +
"inner join fetch order.paymentType.Unknown");
What should come in place of “Unknown”? Since PaymentType class doesn’t know anything about CrediCardNumber class, what can I put in the above query to eagerly load CreditCardNumber.
Thanks in advance.
What you want to do doesn’t work anyway.
In Order you can’t make an entity of type PaymentType and load it with hibernate. PaymentType is abstract and as consequence it can’t be instanciated. Hibernate needs a constructor when loading the entity from so database. So you can’t map abstract classes, you only can map implementing classes which have at least a parameterless constructor.
=> In the mapping of
Orderyou have to specify iforder.paymentTypeisCreditCardPaymentTypeorPaypalPaymentType. And that solves your problem, because then you know whatunknownreally is.To solve your task: At database side you can continue with all payment types in one table. At Java side you have two possibilities (both meant as an idea):
boolean isCreditCardPayment()andboolean isPaypalCardPayment(), and PaymentType contains all columns of the database table. You only use this class and you forget the classesPaypalPaymentTypeandCreditCardPaymentType.or
PaypalPaymentTypeandCreditCardPaymentTypeyou put a where condition ensuring only the rows for the correct payment type are loaded. In Order you drop the property paymentType and as a replacement you add two properties paypalPaymentType and creditcardPaymentType, which are referencing to the two Java classes. When loading theOrder, one of the two properties will be null. For convenience you can make a methodPaymentType getPaymentType(), which returns the not null payment type, but this convenience method you can’t use in HQL queries.