Reputation: 57
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.
Upvotes: 1
Views: 5045
Reputation: 21
Please correct me if I am wrong. Check bellow code
session.createQuery("from Order as order " +
"inner join fetch order.paymentType.class = CreditCardPaymentType");
As per the url,
The special property class accesses the discriminator value of an instance in the case of polymorphic persistence. A Java class name embedded in the where clause will be translated to its discriminator value. from Cat cat where cat.class = DomesticCat
Upvotes: 2
Reputation: 5303
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 Order
you have to specify if order.paymentType
is CreditCardPaymentType
or PaypalPaymentType
. And that solves your problem, because then you know what unknown
really 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()
and boolean isPaypalCardPayment()
, and PaymentType contains all columns of the database table. You only use this class and you forget the classes PaypalPaymentType
and CreditCardPaymentType
.or
PaypalPaymentType
and CreditCardPaymentType
you 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 the Order
, one of the two properties will be null. For convenience you can make a method PaymentType getPaymentType()
, which returns the not null payment type, but this convenience method you can't use in HQL queries.Upvotes: 2