I am developing a JPA app using Hibernate, and I am trying to utilize the strategy pattern in my entities. Basically, I want an order to contain a 1:1 relationship with a payment without knowledge of whether the payment is a credit card payment, check payment, etc. After all, each kind of payment does its own thing, but there is no need for the order to care.
I have an Order entity that owns a 1:1 relation to a Payment as follows:
Code:
@OneToOne
@JoinColumn(name = "ORDER_ID", nullable = false)
public Payment getPayment() {
return payment;
}
public void setPayment(Payment payment) {
this.payment = payment;
}
However, Payment is not an entity. It is just a POJI (with no JPA annotations) that is implemented by an abstract class called PaymentImpl as follows:
Code:
@MappedSuperclass
public abstract class PaymentImpl {
.
.
.
}
Then finally, I have PaymentImpl extended like this:
Code:
@Entity
@Table(name = "CHECK_PAYMENT")
public class CheckPayment extends PaymentImpl {
.
.
.
}
When I do this though, I get this exception:
Code:
org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.myapp.persistence.Order.payment references an unknown entity: com.myapp.persistence.sales.Payment
This makes sense to me since the Payment interface isn't an entity, but how can I maintain this object model in the JPA paradigm?
Thanks for your insight.