JPA

Persistence Operations

Persistence Operations

Persistence operations, which are load and store activities, are used against the database. All persistent operations in a business component are classified as service classes.

Create a package in the hierarchy called 'com.world.EclipseLink.entity' under the src (Source) package. All of the service classes are named Createstudent.java, Updatestudent.java, Findstudent.java, and Deletestudent.java, and they are all part of the given package.

JPA JPQL

Java Persistence Query language

The Java Persistence Query Language (JPQL) is represented in the JPA specification. It is used to build queries against entities that will be stored in a relational database. JPQL was created using SQL syntax. However, it will have no direct impact on the database.

JPQL can use the SELECT clause to retrieve information or data and the UPDATE and DELETE clauses to do massive updates. EntityManager.createQuery() API will support querying language.

Query Structure

JPQL syntax is fairly close to SQL syntax. The SQL-like syntax is advantageous since SQL is a simple structured query language that many developers use in applications. SQL operates on relational database tables, records, and fields, whereas JPQL operates on Java classes and instances.

Scalar and Aggregate Functions

Scalar functions return resultant outputs based on input outputs. Aggregate functions return the resulting values by calculating the input values.

Make a class named ScalarandAggregateFunctions.java under com.world.EclipseLink.service package

After the accumulation and execution of the program, you will receive the result in the console panel of Eclipse IDE.

Between, And, Like Keywords

The primary keywords of JPQL are ‘Between’, ‘And’, and ‘Like’. These keywords are used in a query, following the Where clause.

Create a class named BetweenLikeFunctions.java under com.world.EclipseLink.service package.

Ordering

We use the ORDER BY clause to order the records in JPQL. The use of this clause is identical to that of the SQL clause, but it concerns entities. Follow the Order by example.

Create a class Order.java under com.world.EclipseLink.service package.

Named Queries

@NamedQuery is a query with an unchanging query string predefined. The use of named queries may improve the arrangement of code by separating the JPQL query strings from POJO rather than dynamic queries. Instead of dynamically including literals into the query string, it additionally passes query parameters and results in more efficient queries.

First, add @NamedQuery the employee's entity class annotation within com.world.eclipselink.entity package called student.java.

Create a class NamedQueries.java under com.world.eclipselink.service package.

Eager and Lazy Loading

The main concept of JPA is to duplicate the cache memory of the database. When the database is transacted, it initially affects duplicating data and only changes to the database if it is committed using the entity manager.

There are two ways to get records from the database - eager fetch and lazy fetch.

Eager fetch

Retrieving the entire record while getting the record using Primary Key.

Lazy fetch

It monitors for the availability of reports with the primary key if it is available. Then later if you request any of the getter methods of that entity, it retrieves the complete.

But you can fetch lazy when you first attempt to fetch the record. A copy of the entire record in the cache memory is therefore already saved. Performance-wise, lazy fetch is preferable.

JPA Advanced Mappings

JPA is a library that is released with java specifications. It thus supports all object-oriented principles for the persistence of the entity.

Inheritance Strategies

is the essential concept of an object-oriented language to use inheritance relations and techniques between entities. JPA support three kinds of inheritance approaches as SINGLE_TABLE, JOINED_TABLE, and TABLE_PER_CONCRETE_CLASS.

Single Table strategy

The single-Table approach considers all classes fields (both super and subclasses) and maps them down into one table referred to as the SINGLE_TABLE strategy. Discriminatory value is a significant factor in the distinction of the values of three entities in one table.

Creating Entities

  • Create a package ‘com.world.EclipseLink.entity’ under the ‘src’ package. Make a new java class record.java under the given package. 
  • @DescriminatorColumn defines the field name (type) and the contents of it dispense the remaining fields.
  • Create a subclass (class) to record class named feerecord.java under the com.world.EclipseLink.entity package. 
  • Create a subclass (class) to feerecord class named unpaidfee.java under the com.world.EclipseLink.entity package.

Persistence.xml

Persistence.xml file includes the configuration data of the database and registration data of entity classes. 

Service class

Service classes are part of the business component implementation. Under 'src' the 'com.world.EclipseLink.service' package is created.

Create a class named Savestudent.java under the specified package to store record, feerecord, and unpaidfee class fields.

Joined table Strategy

The joint strategy for the table shares the column mentioned with unique values to join the table and to facilitate transactions.

Create a JPA Project.

Creating Entities

  • Create a package ‘com.world.EclipseLink.entity’ under ‘src’ package. Make a new java class named record.java under the specified package.
  • Make a subclass to record class feerecord.java under the com.world.EclipseLink.entity package.
  • Make a subclass to record class unpaidfee.java under the com.world.EclipseLink.entity package.

Persistence.xml

Persistence.xml file includes the structure information of the database and registration data of entity classes.

Service class

Service classes are part of the business component implementation.

Make a package under the ‘src’ package ‘com.world.EclipseLink.service’.

Create a class Savestudent.java under the specified package to save record, feerecord, and unpaidfee class fields.

After the accumulation and working of the program, you will be notified in the console panel of Eclipse IDE.

Table per class strategy

The table per class approach is to make a table for every sub-entity. The record table will be made, but it will hold null records. The field values of the record table need to be shared by feerecord and unpaidfee tables.

Creating Entities

  • Create a package ‘com.world.EclipseLink.entity’ under ‘src’ package. Make a new java class record.java under the given package.
  • Make a subclass to record class named feerecord.java under the com.world.EclipseLink.entity package.
  • Make a subclass to record class feerecord.java under the com.world.EclipseLink.entity package. 

Persistence.xml

Persistence.xml file has the structure information of database and registration data of entity classes.

Service class

Service classes are part of the business component implementation. Under 'src' the 'com.world.EclipseLink.service' package is created.

Create a class named Savestudent.java under the specified package to store record, feerecord, and unpaidfee class fields.

After the accumulation and working of the program, you will be notified in the console panel of Eclipse IDE.