Listing 1: Person.java, an Abstract Root Entity in a SINGLE_TABLE Inheritance Hierarchy

/*
* Person: An abstract entity, and the root of a SINGLE_TABLE hierarchy
*/
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "TYPE")
@NamedQuery(name = "Person.findAll", query = "select o from Person o")
@SequenceGenerator(name = "PersonIdGenerator",
sequenceName = "CH04_ST_PERSON_SEQ", initialValue = 100,
allocationSize = 20)
@Table(name = "CH04_ST_PERSON")
public abstract class Person
implements Serializable
{
@Column(name = "FIRST_NAME")
private String firstName;
@Id
@Column(nullable = false)
@GeneratedValue(generator="PersonIdGenerator")
private Long id;
@Column(name = "LAST_NAME")
private String lastName;
@Version
private Long version;
@OneToOne(cascade = { CascadeType.ALL })
@JoinColumn(name = "HOME_ADDRESS", referencedColumnName = "ID")
private Address homeAddress;
public Person() {
}
/* get/set methods... */
}


Listing 2:  Employee.java, an Abstract Intermediate Entity in a SINGLE_TABLE Inheritance Hierarchy

/*
* Employee: An abstract entity
*/
@Entity
@NamedQuery(name = "Employee.findAll", query = "select o from Employee o")
public abstract class Employee
extends Person
implements Serializable
{
protected String email;
protected String dept;
@ManyToOne(cascade = { CascadeType.ALL })
@JoinColumn(name = "MANAGER", referencedColumnName = "ID")
protected FullTimeEmployee manager;
public Employee() {
}
/* get/set methods... */
}


Listing 3: FullTimeEmployee.java, a Concrete Leaf Entity in a SINGLE_TABLE Inheritance Hierarchy

/*
* FullTimeEmployee: A concrete leaf entity */
@Entity
@NamedQuery(name = "FullTimeEmployee.findAll",
query = "select o from FullTimeEmployee o")
@DiscriminatorValue("FullTimeEmployee") // Illustrating the default value
public class FullTimeEmployee
extends Employee
implements Serializable
{

@Column(name = "ANNUAL_SALARY")
protected Double annualSalary;
@OneToMany(mappedBy = "manager", cascade = { CascadeType.ALL })
public List managedEmployees;

public FullTimeEmployee() {
}
/* get/set methods... */
}


Listing 4: PartTimeEmployee.java, a Concrete Leaf Entity in a SINGLE_TABLE Inheritance Hierarchy

/*
* PartTimeEmployee: A concrete leaf entity
*/
@Entity
@NamedQuery(name = "PartTimeEmployee.findAll",
query = "select o from PartTimeEmployee o")
public class PartTimeEmployee
extends Employee
implements Serializable
{
@Column(name = "HOURLY_WAGE")
protected Double hourlyWage;
public PartTimeEmployee() {
}
/* get/set methods... */
}


Listing 5:  Person.java, an Abstract Root Entity in a JOINED Inheritance Hierarchy

/*
* Person: An abstract entity, and the root of a JOINED hierarchy
*/
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "TYPE")
@NamedQuery(name = "Person.findAll", query = "select o from Person o")
@SequenceGenerator(name = "PersonIdGenerator",
sequenceName = "CH04_JOIN_PERSON_SEQ", initialValue = 100,
allocationSize = 20)
@Table(name = "CH04_JOIN_PERSON")
public abstract class Person
implements Serializable
{
/* The class body is identical across all inheritance strategies */
}


Listing 6: Person.java, an Abstract Root Entity in a TABLE_PER_CLASS Inheritance Hierarchy

/*
* Person: An abstract entity, and the root of a TABLE_PER_CLASS hierarchy
*/
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@DiscriminatorColumn(name = "TYPE")
@NamedQuery(name = "Person.findAll", query = "select o from Person o")
@SequenceGenerator(name = "PersonIdGenerator",
sequenceName = "CH04_TPC_PERSON_SEQ", initialValue = 100,
allocationSize = 20)
@Table(name = "CH04_TPC_PERSON")
public abstract class Person
implements Serializable
{
/* The class body is identical across all inheritance strategies */
}