The
JPA2 spec has this as an example in section
2.11.1 Abstract Entity Classes:
Code:
@Entity
@Table(name="EMP")
@Inheritance(strategy=JOINED)
public abstract class Employee {
@Id protected Integer empId;
@Version protected Integer version;
@ManyToOne protected Address address;
...
}
@Entity
@Table(name="FT_EMP")
@DiscriminatorValue("FT")
@PrimaryKeyJoinColumn(name="FT_EMPID")
public class FullTimeEmployee extends Employee {
// Inherit empId, but mapped in this class to FT_EMP.FT_EMPID
// Inherit version mapped to EMP.VERSION
// Inherit address mapped to EMP.ADDRESS fk
// Defaults to FT_EMP.SALARY
protected Integer salary;
...
}
@Entity
@Table(name="PT_EMP")
@DiscriminatorValue("PT")
// PK column is PT_EMP.EMPID due to PrimaryKeyJoinColumn default
public class PartTimeEmployee extends Employee {
protected Float hourlyWage;
...
}
If you're not using @DiscriminatorValue and are simply refactoring common entity patterns out into an abstract base class, you'll want to have a look at @AttributeOverrides and @AttributeOverride for differing column names (e.g. your @Id property in your abstract base class maps to two different column names depending on the table). So you'd stick those @AttributeOverrides in your subclasses in case you need to tweak the mappings a bit. Other than that, you need to specify that you want to include mappings in your abstract base class by marking it with @MappedSuperclass (actually the next section 2.11.2 in the spec). That whole chapter is actually pretty good and should cover your specific use case better than I could.