JPA Advanced Mappings - JAVA Persistence API (JPA)

What is JPA - Advanced Mappings?

JPA is known as a library developed for java specification. JPA supports all object oriented concepts to make entity persistence. In previous chapters we have discussed about the basics of object relational mapping. This topic deals about advanced mappings between objects and relational entities.

Inheritance Strategies

Inheritance is key concept of object oriented language so inheritance relationships or strategies can be used in between entities. JPA provides support to three types of inheritance strategies like SINGLE_TABLE, JOINED_TABLE, and TABLE_PER_CONCRETE_CLASS.
Let’s see following example of Staff, TeachingStaff, NonTeachingStaff classes and their relationships as follows
jpa 20
The above image explains about Staff is an entity and TeachingStaff and NonTeachingStaff are the sub entities of Staff. Let’s discuss the above images with all three strategies of inheritance.

Single Table strategy

Single-Table strategy includes all classes fields (both super and sub classes) and create them down with a single table known as SINGLE_TABLE strategy. Make sure that discriminator value is an important element in differentiating the values of three entities in one table.
Let’s observe the above example, TeachingStaff and NonTeachingStaff are the sub classes of class Staff. Now remind the concept of inheritance (is a mechanism of inheriting the properties of super class by sub class) and therefore sid, sname are the fields including both TeachingStaff and NonTeachingStaff. Create a JPA project. All the modules of this project as follows

Creating Entities

Now develop a package named ‘com.wisdomjobs.eclipselink.entity’ with ‘src’package. Let’s create a new java class with Staff.java under given package. The Staff entity class is shown as follows:
The above mentioned code @DescriminatorColumn describes about the field name (type)and the values of it shows the remaining (Teaching and NonTeachingStaff) fields.
Create a subclass (class) to Staff class named TeachingStaff.java under the com.wisdomjobs.eclipselink.entity package. The TeachingStaff Entity class is shown as follows:
Create a subclass (class) to Staff class named NonTeachingStaff.java under the com.wisdomjobs.eclipselink.entity package. The NonTeachingStaff Entity class is shown as follows:

Persistence.xml

Persistence.xml file includes the configuration information of database and registration information of entity classes. The xml file is as mentioned below

Service class

Service classes are considered as the implementation part of business component. Let’s create a package under ‘src’ package named ‘com.wisdomjobs.eclipselink.service’.
Now create a class named SaveClient.java with the given package to store Staff, TeachingStaff, and NonTeachingStaff class fields. The SaveClient class is shown as follows:
Once you compile and execution of the above program you will get notifications in the console panel of Eclipse IDE. Check MySQL workbench for output. Following table contains the output shown as below:
Sid
Type
Sname
Areaexpertise
Qualification
Subjectexpertise
1
TS
Gopal
MSC MED
Maths
2
TS
Manisha
BSC BED
English
3
NS
Satish
Accounts
4
NS
Krishna
Office Admin
Now you can get single table which includes all three class’s fields and differs with discriminator column named ‘Type’ (field).

Joined table Strategy

Joined table strategy is meant to be shared the referenced column which includes unique values to create a table and make easy transactions. Let’s see the same example as above.
Create a JPA Project. All the project modules shown as follows

Creating Entities

Now create a package named ‘com.wisdomjobs.eclipselink.entity’ under ‘src’package. Create a new java class named Staff.java under given package. The Staff entity class is shown as follows:
Let’s create a subclass (class) to Staff class named TeachingStaff.java with the com.wisdomjobs.eclipselink.entity package. The TeachingStaff Entity class is shown as follows
Now create a subclass (class) to Staff class named NonTeachingStaff.java under the com.wisdomjobs.eclipselink.entity package. The NonTeachingStaff Entity class is as mentioned below:

Persistence.xml

Persistence.xml file includes the information of configuration database and registration information of entity classes. The xml file is as mentioned below:

Service class

Service classes are known as the implementation part of business component. Let’s create a package under ‘src’ package named ‘com.wisdomjobs.eclipselink.service’.
Now create a class named SaveClient.java under the given package to store Staff, TeachingStaff, and NonTeachingStaff class fields. Then SaveClient class as follows
Once you compile and execute the above program you will get notifications in the console panel of Eclipse IDE. Then check output of MySQL workbench as follows:
Here three tables are created and the result of staff table in a tabular format is shown as follows:
Sid
Dtype
Sname
1
TeachingStaff
Gopal
2
TeachingStaff
Manisha
3
NonTeachingStaff
Satish
4
NonTeachingStaff
Krishna
The result of TeachingStaff table in a tabular format is shown as follows:
Sid
Qualification
Subjectexpertise
1
MSC MED
Maths
2
BSC BED
English
In the above table sid is the foreign key (reference field form staff table) The result of NonTeachingStaff table in tabular format is shown as follows:
Sid
Areaexpertise
3
Accounts
4
Office Admin
You can see that the three tables are created with their respective fields and SID field is shared by all three tables. While staff table SID is primary key, in remaining (TeachingStaff and NonTeachingStaff) tables SID is foreign key.

Table per class strategy

Table per class strategy is used to create a table for each sub entity. The staff table will be created but it contains null records. The field values of Staff table must be shared by TeachingStaff and NonTeachingStaff tables.
Let ‘s see the same example as mentioned above. All modules of this project are shown as follows:

Creating Entities

Create a package named ‘com.wisdomjobs.eclipselink.entity’ under ‘src’package. Create a new java class named Staff.java under given package. The Staff entity class is shown as follows:
Now create a subclass (class) to Staff class named TeachingStaff.java under the com.wisdomjobs.eclipselink.entity package. The TeachingStaff Entity class is mentioned below:
Let’s create a subclass (class) to Staff class named NonTeachingStaff.java under the com.wisdomjobs.eclipselink.entity package. The NonTeachingStaff Entity class is shown as follows

Persistence.xml

Persistence.xml file includes the configuration information of database and registration information of entity classes. The xml file is as mentioned below:

Service class

Service classes are known as the implementation part of business component. Now create a package under ‘src’ package named ‘com.wisdomjobs.eclipselink.service’.
Let’s 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:
Once you compile and execute the above program you will get notifications in the console panel of Eclipse IDE. For output, check MySQL workbench as follows:
Let’s see the following three tables and the Staff table with 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.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

JAVA Persistence API (JPA) Topics