Home » JPA Joined Strategy

JPA Joined Strategy

by Online Tutorials Library

JPA Joined strategy

In joined strategy, a separate table is generated for every entity class. The attribute of each table is joined with the primary key. It removes the possibility of duplicacy.

The following syntax represents the joined strategy: –

Joined 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 variables 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

After the execution of program, the following directory hierarchy is generated under the MySQL workbench.

JPA Joined strategy

Output:

Now, fetch data from each table separately to generate the output.

  • Select * from employee_details

JPA Joined strategy

  • Select * from active_employee

JPA Joined strategy

  • Select * from retired_employee

JPA Joined strategy

You may also like