Creating Entity Objects for TABLE_PER_CLASS Inheritance Type

This inheritance mapping option maps each outermost concrete entity to its own dedicated table. Each table maps all of the fields in that entity's entire type hierarchy; since there's no shared table, no columns are shared. The only table structure requirement is that all tables must share a common primary key structure, meaning that the name(s) and type(s) of the primary key column(s) must match across all tables in the hierarchy.

The tables are required to share nothing in common except the structure of their primary key, and since the table implicitly identifies the entity type, no discriminator column is required. With this inheritance mapping strategy, only concrete entities require tables.

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.tableperclass;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;

@Entity(name="Person_TABLE_PER_CLASS")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@Table(name = "PERSON_TABLE_PER_CLASS")
public class Person {

	@Id
	@Column(nullable = false)
	@GeneratedValue(strategy=GenerationType.TABLE)
	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;
	}
}
					

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.tableperclass;

import javax.persistence.Entity;
import javax.persistence.Table;

@Entity(name="Employee_TABLE_PER_CLASS")
@Table(name = "EMPLOYEE_TABLE_PER_CLASS")
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