Inheritance strategy is defined by the InheritanceType enum:
public enum InheritanceType { SINGLE_TABLE, JOINED, TABLE_PER_CLASS }
SINGLE_TABLE: Single table per class inheritance hierarchy. This is the default strategy. The entity hierarchy is essentially flattened into the sum of its fields, and these fields are mapped down to a single table.
JOINED: Common base table, with joined subclass tables. In this approach, each entity in the hierarchy maps to its own dedicated table that maps only the fields declared on that entity. The root entity in the hierarchy is known as the base table, and the tables for all other entities in the hierarchy join with the base table.
TABLE_PER_CLASS: Single table per outermost concrete entity class. This strategy maps each leaf (i.e., outermost, concrete) entity to its own dedicated table. Each such leaf entity branch is flattened, combining its declared fields with the declared fields on all of its super-entities, and the sum of these fields is mapped onto its table.
The default inheritance mapping strategy is SINGLE_TABLE in which all the entities in the class hierarchy map onto a single table. A dedicated discriminator column on this table identifies the specific entity type associated with each row, and each entity in the hierarchy is given a unique value to store in this column. By default, the discriminator value for an entity is its entity name, although an entity may override this value using the @DiscriminatorValue annotation. This approach performs well for querying, since only a single table is involved.
Create Person EJB 3.0 Entity as described in Creating An Entity Object (POJO) section:
Source folder: InheritanceHierarchies/src
Package: by.iba.ejb3.singletable
Name: Person
Copy-paste code below in the class body:
package by.iba.ejb3.singletable; import javax.persistence.Column; import javax.persistence.DiscriminatorColumn; import javax.persistence.DiscriminatorType; import javax.persistence.DiscriminatorValue; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.Table; @Entity(name="Person_SINGLE_TABLE") @Inheritance(strategy = InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name = "TYPE", discriminatorType=DiscriminatorType.STRING) @DiscriminatorValue("PERSON") @Table(name = "PERSON_SINGLE_TABLE") public class Person { @Id @Column(nullable = false) @GeneratedValue private Long id; @Column(name = "FIRST_NAME") private String firstName; @Column(name = "LAST_NAME") private String lastName; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } }
By default, the persistence manager looks for a column named DTYPE in the root entity's table (PERSON, in this case). In our example, we have named the discriminator column TYPE, so we explicitly annotate this setting, using the @DiscriminatorColumn(name = "TYPE") annotation. Were we to use a column named DTYPE, we could have skipped this annotation altogether and accepted the default value.
Now create Employee EJB 3.0 Entity as described in Creating An Entity Object (POJO) section:
Source folder: InheritanceHierarchies/src
Package: by.iba.ejb3.singletable
Name: Employee
Superclass: by.iba.ejb3.singletable.Person
Copy-paste code below in the class body:
package by.iba.ejb3.singletable; import javax.persistence.DiscriminatorValue; import javax.persistence.Entity; @Entity(name="Employee_SINGLE_TABLE") @DiscriminatorValue("EMPLOYEE") public class Employee extends Person { private String department; private String email; public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
Now InheritanceHierarchies EJB 3.0 project should look like this:
![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |