Javatpoint Logo
Javatpoint Logo

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.javatpoint.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.javatpoint.jpa.inheritence package.

ActiveEmployee.java

  • Create another entity class RetiredEmployee.java (subclass of Employee.java) under com.javatpoint.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.javatpoint.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




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA