Creating Entity Objects for SINGLE_TABLE Inheritance Type

Inheritance strategy is defined by the InheritanceType enum:

public enum InheritanceType {
	SINGLE_TABLE, 
	JOINED, 
	TABLE_PER_CLASS
}
					

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:

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:

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:

InheritanceHierarchies EJB 3.0 project

Professional hosting     Belorussian informational portal         Free SCWCD 1.4 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide     Free Mock Exam Engine