
JPA - Persistence Operations
- JPA - Entity Managers
- JPA - Create Employee Example
- JPA - Update Employee Example
- JPA - Find Employee Example
- JPA - Delete Employee Example
- JPA - Criteria API
JPA - JPQL
- JPA - JPQL
- JPA - Scalar Function
- JPA - Aggregate Function
- JPA - Between Keyword
- JPA - Like Keyword
- JPA - Order By Clause
- JPA - Named Query
JPA - Advanced Mappings
- JPA - Advanced Mappings
- JPA - Single Table Strategy
- JPA - Joined Table Strategy
- JPA - Table per Class Strategy
JPA - Entity Relationships
- JPA - Entity Relationships
- JPA - @ManyToOne Relationships
- JPA - @OneToMany Relationships
- JPA - @OneToOne Relationships
- JPA - @ManyToMany Relationships
JPA - Useful Resources
JPA - Table Per Class Strategy
Overview
Table per class strategy is to create a table for each sub entity. The staff table will be created but it will contain null records. The field values of Staff table must be shared by TeachingStaff and NonTeachingStaff tables.Let us reconsider the example discussed in Joined Table Strategy chapter.
All modules of this project are shown as follows:
Creating Entities
Create a package named com.tutorialspoint.eclipselink.entity under src package if not present. Create a new java class named Staff.java under given package. The Staff entity class is shown as follows:
Staff.java
package com.tutorialspoint.eclipselink.entity; import java.io.Serializable; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.Inheritance; import jakarta.persistence.InheritanceType; import jakarta.persistence.Table; @Entity @Table @Inheritance( strategy = InheritanceType.TABLE_PER_CLASS ) public class Staff implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue( strategy = GenerationType.AUTO ) private int sid; private String sname; public Staff( int sid, String sname ) { super( ); this.sid = sid; this.sname = sname; } public Staff( ) { super( ); } public int getSid( ) { return sid; } public void setSid( int sid ) { this.sid = sid; } public String getSname( ) { return sname; } public void setSname( String sname ) { this.sname = sname; } }
Create a subclass (class) to Staff class named TeachingStaff.java under the com.tutorialspoint.eclipselink.entity package. The TeachingStaff Entity class is shown as follows:
TeachingStaff.java
package com.tutorialspoint.eclipselink.entity; import jakarta.persistence.Entity; @Entity public class TeachingStaff extends Staff { private static final long serialVersionUID = 1L; private String qualification; private String subjectexpertise; public TeachingStaff( int sid, String sname, String qualification, String subjectexpertise ) { super( sid, sname ); this.qualification = qualification; this.subjectexpertise = subjectexpertise; } public TeachingStaff( ) { super( ); } public String getQualification( ){ return qualification; } public void setQualification( String qualification ) { this.qualification = qualification; } public String getSubjectexpertise( ) { return subjectexpertise; } public void setSubjectexpertise( String subjectexpertise ){ this.subjectexpertise = subjectexpertise; } }
Create a subclass (class) to Staff class named NonTeachingStaff.java under the com.tutorialspoint.eclipselink.entity package. The NonTeachingStaff Entity class is shown as follows:
NonTeachingStaff.java
package com.tutorialspoint.eclipselink.entity; import jakarta.persistence.Entity; @Entity public class NonTeachingStaff extends Staff { private static final long serialVersionUID = 1L; private String areaexpertise; public NonTeachingStaff( int sid, String sname, String areaexpertise ) { super( sid, sname ); this.areaexpertise = areaexpertise; } public NonTeachingStaff( ) { super( ); } public String getAreaexpertise( ) { return areaexpertise; } public void setAreaexpertise( String areaexpertise ) { this.areaexpertise = areaexpertise; } }
Persistence.xml
Persistence.xml file contains the configuration information of database and registration information of entity classes. The xml file is shown as follows:
<?xml version="1.0" encoding="UTF-8"?> <persistence version="3.0" xmlns="https://jakarta.ee/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence https://jakarta.ee/xml/ns/persistence/persistence_3_0.xsd"> <persistence-unit name="Eclipselink_JPA" transaction-type="RESOURCE_LOCAL"> <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> <class>com.tutorialspoint.eclipselink.entity.Staff</class> <class>com.tutorialspoint.eclipselink.entity.NonTeachingStaff</class> <class>com.tutorialspoint.eclipselink.entity.TeachingStaff</class> <properties> <property name="jakarta.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/jpadb"/> <property name="jakarta.persistence.jdbc.user" value="guest"/> <property name="jakarta.persistence.jdbc.password" value="guest123"/> <property name="jakarta.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/> <property name="eclipselink.logging.level" value="FINE"/> <property name="eclipselink.ddl-generation" value="create-tables"/> </properties> </persistence-unit> </persistence>
Service class
Service classes are the implementation part of business component. Create a package under src package named com.tutorialspoint.eclipselink.service.
Create a class named SaveClient.java under the given package to store Staff, TeachingStaff, and NonTeachingStaff class fields. The SaveClient class is shown as follows:
SaveClient.java
package com.tutorialspoint.eclipselink.service; import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManagerFactory; import jakarta.persistence.Persistence; import com.tutorialspoint.eclipselink.entity.NonTeachingStaff; import com.tutorialspoint.eclipselink.entity.TeachingStaff; public class SaveClient { public static void main( String[ ] args ) { EntityManagerFactory emfactory = Persistence.createEntityManagerFactory( "Eclipselink_JPA" ); EntityManager entitymanager = emfactory.createEntityManager( ); entitymanager.getTransaction( ).begin( ); //Teaching staff entity TeachingStaff ts1 = new TeachingStaff(1,"Gopal","MSc MEd","Maths"); TeachingStaff ts2 = new TeachingStaff(2, "Manisha", "BSc BEd", "English"); //Non-Teaching Staff entity NonTeachingStaff nts1 = new NonTeachingStaff(3, "Satish", "Accounts"); NonTeachingStaff nts2 = new NonTeachingStaff(4, "Krishna", "Office Admin"); //storing all entities entitymanager.persist(ts1); entitymanager.persist(ts2); entitymanager.persist(nts1); entitymanager.persist(nts2); entitymanager.getTransaction().commit(); entitymanager.close(); emfactory.close(); } }
Output
After compilation and execution of the above program you will get notifications in the console panel of Eclipse IDE. For output, check MySQL workbench as follows:
Here the three tables are created and the Staff table contains null records.
The result of TeachingStaff in a tabular format is shown as follows:
Sid | Qualification | Sname | Subjectexpertise |
---|---|---|---|
1 | MSC MED | Gopal | Maths |
2 | BSC BED | Manisha | English |
The above table TeachingStaff contains fields of both Staff and TeachingStaff Entities.
The result of NonTeachingStaff in a tabular format is shown as follows:
Sid | Areaexpertise | Sname |
---|---|---|
3 | Accounts | Satish |
4 | Office Admin | Krishna |
The above table NonTeachingStaff contains fields of both Staff and NonTeachingStaff Entities.