Home » JPA Single Table Strategy

JPA Single Table Strategy

by Online Tutorials Library

JPA Single Table Strategy

The single table strategy is one of the most simplest and efficient way to define the implementation of inheritance. In this approach, instances of the multiple entity classes are stored as attributes in a single table only.

The following syntax represents the single table strategy: –

Single Table Strategy Example

In this example, we will categorize employees into active employees and retired employees.

Thus, the subclass ActiveEmployees and RetiredEmployees inherits the e_id and e_name fields of parent class Employee.

Now, follow the below steps to create JPA project: –

  • Create a root entity class Employee.java under the com.tutoraspire.jpa.inheritence package and specify all the required attributes and annotations.

Employee.java

  • Create an entity class ActiveEmployee.java (subclass of Employee.java) under com.tutoraspire.jpa.inheritence package.

ActiveEmployee.java

  • Create another entity class RetiredEmployee.java (subclass of Employee.java) under com.tutoraspire.jpa.inheritence package.

RetiredEmployee.java

  • Now, map the entity class and other databases confiuguration in Persistence.xml file.

Persistence.xml

  • Create a persistence class EmployeePersistence.java under com.tutoraspire.jpa.persistence package. This class is used to initialize an object and persist it.

EmployeePersistence.java

Output:

After the execution of the program, run select * from employee_details query in MySQL database to generate the following output: –

JPA Single Table Strategy

You may also like