In the book "Java Persistence with Hibernate, Second Edition", section 6.2 "Table per concrete class with unions" (p.120) it describes one way to map class inheritance.
The entities are:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class BillingDetails {
@Id
@GeneratedValue(generator = Constants.ID_GENERATOR)
protected Long id;
@NotNull
protected String owner;
// ...
}
and
@Entity
public class CreditCard extends BillingDetails {
@NotNull
protected String cardNumber;
@NotNull
protected String expMonth;
@NotNull
protected String expYear;
// ...
}
The SQL created to select from the tables is:
select
ID, OWNER, EXPMONTH, EXPYEAR, CARDNUMBER,
ACCOUNT, BANKNAME, SWIFT, CLAZZ_
from
( select
ID, OWNER, EXPMONTH, EXPYEAR, CARDNUMBER,
null as ACCOUNT,
null as BANKNAME,
null as SWIFT,
1 as CLAZZ_
from
CREDITCARD
union all
select
id, OWNER,
null as EXPMONTH,
null as EXPYEAR,
null as CARDNUMBER,
ACCOUNT, BANKNAME, SWIFT,
2 as CLAZZ_
from
BANKACCOUNT
) as BILLINGDETAILS
So far the table schema is clear to me.
Now, I want to extend this schema. I want to add a new entity, Purchase, so that there are many Purchases for every BillingDetails.
Is it possible to implement it while keeping the basic schema of the BillingDetails hierarchy?
If it's possible, can you describe the tables of the extended schema?
I'm not necessarily interested in a way to describe the schema by Hibernate annotations/XML (in case that it cannot be mapped by Hibernate annotations/XML), but in the basic table structure and related SQL that can describe such a relationship.