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: -

@Inheritance(strategy=InheritanceType.JOINED)  

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: -

Step 1.  Create a root entity class Employee.java under the com.javatpoint.jpa.inheritence package and specify all the required variables and annotations.

Employee.java

package com.javatpoint.jpa.inheritence; 
import java.io.Serializable;
import javax.persistence.*;  
  
@Entity  
@Table(name="employee_details")  
@Inheritance(strategy=InheritanceType.JOINED)  
public class Employee implements Serializable {  
      
@Id       
private int e_id;  
private String e_name;
  
public Employee(int e_id, String e_name) {  
    super();  
    this.e_id = e_id;  
    this.e_name = e_name;  
}  
 
public Employee() {  
    super();   
}  
 
public int getE_id() {  
    return e_id;  
}  
 
public void setE_id(int e_id) {  
    this.e_id = e_id;  
}  
 
public String getE_name() {  
    return e_name;  
}  
 
public void setE_name(String e_name) {  
    this.e_name = e_name;  
}
}  

Step 2.  Create an entity class ActiveEmployee.java (subclass of Employee.java) under com.javatpoint.jpa.inheritence package.

ActiveEmployee.java

package com.javatpoint.jpa.inheritence;  
import javax.persistence.*;  
  
@Entity  
public class ActiveEmployee extends Employee {  
  
    private int e_salary;  
    private int e_experience;  
    public ActiveEmployee(int e_id, String e_name, int e_salary, int e_experience) {  
        super(e_id, e_name);  
        this.e_salary = e_salary;  
        this.e_experience = e_experience;  
    }  
    public ActiveEmployee() {  
        super(); 
    }  
    public int getE_salary() {  
        return e_salary;  
    }  
    public void setE_salary(int e_salary) {  
        this.e_salary = e_salary;  
    }  
    public int getE_experience() {  
        return e_experience;  
    }  
    public void setE_experience(int e_experience) {  
        this.e_experience = e_experience;  
    }   
}

Step 3.  Create another entity class RetiredEmployee.java (subclass of Employee.java) under com.javatpoint.jpa.inheritence package.

RetiredEmployee.java

package com.javatpoint.jpa.inheritence; 
import javax.persistence.*;

@Entity  
public class RetiredEmployee extends Employee {  
  
    private int e_pension;  
      
    public RetiredEmployee(int e_id, String e_name, int e_pension) {  
        super(e_id, e_name);  
        this.e_pension = e_pension;  
    }  
  
    public RetiredEmployee() {  
        super();  
      
    }  
 
    public int getE_pension() {  
        return e_pension;  
    }  
  
    public void setE_pension(int e_pension) {  
        this.e_pension = e_pension;  
    }   
}

Step 4.  Now, map the entity class and other databases configuration in Persistence.xml file.

Persistence.xml

<persistence>  
<persistence-unit name="Employee_details">  
     
      <class>com.javatpoint.jpa.inheritence.ActiveEmployee</class>  
      <class>com.javatpoint.jpa.inheritence.RetiredEmployee</class>  
      <class>com.javatpoint.jpa.inheritence.Employee</class> 
        
     <properties>  
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>  
         <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/employee"/>  
         <property name="javax.persistence.jdbc.user" value="root"/>  
         <property name="javax.persistence.jdbc.password" value=""/>  
         <property name="eclipselink.logging.level" value="SEVERE"/>  
         <property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>  
      </properties>  
      </persistence-unit>  
      </persistence>  

Step 5.  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

package com.javatpoint.jpa.persistence;  
  
import javax.persistence.*;  
import com.javatpoint.jpa.inheritence.*;  
  
public class EmployeePersistence {  
      
    public static void main(String[] args) {
  
    EntityManagerFactory emf=Persistence.createEntityManagerFactory("Employee_details");  
    EntityManager em=emf.createEntityManager();  
      
    em.getTransaction().begin();
      
ActiveEmployee ae1=new ActiveEmployee(101,"Karun",10000,5);  
ActiveEmployee ae2=new ActiveEmployee(102,"Rishab",12000,7);  
  
RetiredEmployee re1=new RetiredEmployee(103,"Ramesh",5000);  
RetiredEmployee re2=new RetiredEmployee(104,"Raj",4000);  
  
    em.persist(ae1);  
    em.persist(ae2);  
      
    em.persist(re1);  
    em.persist(re2);  
      
    em.getTransaction().commit();  
      
    em.close();  
    emf.close();  
      
}}

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






Output:

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

Select * from employee_details







Select * from active_employee





Select * from retired_employee