JPA

ORM Architecture

ORM Architecture

Phase 1

The first phase, the data phase Object, comprises POJOs (PLAIN OLD JAVA OBJECT), interfaces and classes. It is the main layer of business components with activities and attributes of business logic.

For example, let us understand with student database as schema-

  • Student POJO class includes properties such as id, name, salary and designation. And methods such as setter and getter of these characteristics.
  • DAO/Service student classes contain service methods like create students, find students and delete students.

Phase 2

This second phase is referred to mapping or persistence phase, which contains the JPA provider, mapping file (ORM.xml), JPA Loader, and Object Grid.

  • JPA Provider

The vendor product includes JPA flavour (javax. persistence). For example, Eclipselink, Toplink, Hibernate, etc.

  • Mapping file

Mapping data in the POJO class and data on a relational data basis is part of the mapping file (ORM.xmL).

  • JPA Loader

The JPA loader works as a cache so that the relational grid data can be loaded. It functions as a copy of a database to communicate with POJO data services classes (Characteristics of POJO class).

  • Object Grid

The Object grid is a temporary space where the relational data copy can be stored, i.e. a memory. The data in the object grid first affects all queries against the database. It impacts the primary database only once it is perpetrated.

Phase 3

The third phase consists of the relational data phase. It holds the related data that is logically linked to the company component. The data is only physically stored in the database when the business component commits the data. The changed data will be stored in a grid format in cache memory. The same applies to the process of data collection.

The mechanism of the programmatic interaction is called object-relational mapping.

 

Mapping.xml

A.xml mapping is to instruct the JPA vendor to use database tables to map Entity classes.

The script for mapping the entity class with a database table are:

  • <entity-mappings> 

The tag specifies the schema description to enable entity tags into the XML file.

  • <description>

 The tag describes the description of the application.

  • <entity>

The tag describes the entity class which you need to change into a table in a database. Attribute class explains the POJO (PLAIN OLD JAVA OBJECT) entity class name.

  • <table> 

The tag describes the table name. This tag is not necessary if you have to retain the class name as a table name.

  • <attributes> 

The tag describes the characteristics (fields in a table).

  • <id>

The tag describes the table's main key. The <generated-value> tag describes how the primary value like Automatic, Manual or Sequence should be assigned to the primary value.

  • <basic>

The tag is used for describing the remaining attributes for the table.

  • <column-name> 

The tag is used to describe the user-defined table field name.

Annotations

XML files are generally used to define the particular component or to map two separate component descriptions. This means that we have to check POJO class characteristics with the entity tags of the mapping.xml file when writing an XML mapping file.

We can write the configuration phase with annotations in the class definition. Annotations of classes, properties and methods are used. Start with the '@' symbol for annotations. Before the class, property or function is declared, annotations are declared. In javax. persistence package, all JPA annotations have been defined.

Java Bean Standard

Java class encapsulates instance values and conduct them into one object unit. Java Bean is a reusable component or objects temporary storage system. It's a serialisable class with default constructors and getter & setter methods to initialise the instance characteristics independently.

Java Bean Conventions

  • Bean has the default constructor or a serialised instance file. A bean can hence instantiate the bean.
  • Boolean properties and non- Boolean properties can be divided into boolean and non-boolean properties.
  • The non-boolean property has both methods of getter and setter.
  • The boolean attribute has a setter and is a method.
  • A small lettered "get" (Java method convention), which starts with a field name, should begin with the getter method of any property and then continue. For example, the field name is 'income'; therefore, this field is 'getincome ()' as the getter method.
  • A small lettered 'set' (java method convention) should begin with the setter procedure for property and use a field name that starts with a capital letter and the parameter value to set to a field. For example, the field name is 'income,' thus 'setincome (double inc)' is the setter method of this field.
  • A method to check whether the property is true or false is a boolean property. E.g. the 'empty' Boolean property, this 'isEmpty()' method is.

JPA Installation

This lesson helps you to set up JPA on systems based on Windows and Linux. JPA can be quickly installed and incorporated into your existing Java environment following a few basic steps without complex settings. During installation, user administration is required.

 

Let us now proceed with the steps to install JPA:

Step 1: Verify your Java Installation

Step 2: Check the status of your Java installation.

First and foremost, your system must have the Java Software Development Kit (SDK) installed. To confirm this, run either of the two commands listed below, depending on the platform you're using.

For Windows: Open command console and type:

\>java –version

For Linux: Open command terminal and type:

$java –version

Step 3: Set your Java Environment

Set the environment variable JAVA_World to the base directory location on your machine where Java is installed.

Step 4: Installing JPA

Use any of the JPA Providers for JPA installation, E.g. Eclipselink, Hibernate. Follow the JPA installation using Eclipselink. For JPA programming, we have to follow the particular folder structure; therefore, it is better to use IDE.

Download the Eclipse IDE. Eclipse Indigo is the EclipseIDE for JavaEE developers.

Unzip the Eclipse zip file to your C drive. Launch the Eclipse IDE

Installing JPA using Eclipselink

Eclipselink is a library; therefore, we cannot add it directly to Eclipse IDE. For installing JPA using Eclipselink, you need to follow the following steps.

  • Make a new JPA project by clicking on File then New and JPA Project in the Eclipse IDE.
  • A dialogue box will appear named New JPA Project. Enter project name ‘java_JPA_Eclipselink, check the JRE version and proceed to the next step.
  • Click on the download library in the user library section.
  • Choose the advanced version of the Eclipselink library in the Download Library dialogue box and click next.
  • Agree with the terms and conditions of the license, then click Finish to download the library.
  • The downloading process will start.
  • After downloading, go to the user library section and select the downloaded library, then click finish.
  • Finally, the project file appears in the Eclipse IDE's Package Explorer. You will get the folder and file hierarchy from there extract all the files.

Adding MySQL connector to Project

Let us take an example, consider MySQL database for database operations. It needs a MySQL-connector jar to communicate with the java program.

  • To configure the database jar for the project, follow the steps below.
  • Right-click on the project and select Properties -> Java Build Path. A dialogue box will appear Select Add External Jars.
  • Navigate to the jar location in your system memory, then select and click on open.
  • On the properties dialogue, click OK. The MySQL-connector Jar will be added to the project. You can now perform database operations with MySQL.

JPA Entity Manager

In general, an entity is a collection of states that are linked together to form a single unit. When behaviour is added to an entity, it behaves like an object and becomes a key component of the object-oriented paradigm. In Java Persistence Library, an entity is an application-defined object.

Entity Properties

 

The following are the properties of an entity that an object must possess: 

  • Persistability 

A persistent object is stored in a database and can be accessed at any time.

  • Persistent Identity 

In Java, each entity is distinct and represents an object's identity. Similarly, when an object's identity is saved in a database, it is interpreted as a persistence identity. This object identity corresponds to the primary key in the database.

  • Transactionality 

An entity can carry out a variety of operations such as create, delete, and update. Each operation modifies the database in some way. It ensures that any changes made to the database succeed or fail atomically.

  • Granuality 

Entities with a single-dimensional state should not be primitives, primitive wrappers, or built-in objects.

Entity Metadata

Each entity is linked to some metadata that describes its data. Instead of a database, this metadata can be seen both inside and outside of the class. This metadata can exercise the following forms:

  • Annotation

Annotations are a type of tag in Java that represents metadata. This metadata persists within the class.

  • XML

Metadata persists outside of the class in the XML file.

Creating an Entity

A Java class can easily be converted into an entity. The following are the fundamental prerequisites for transformation:

  • No-argument Constructor
  • Annotation

Entities are simply beans or Models. It includes default constructor, setter and getter functions of those characteristics.

Create a package in the hierarchy called 'com.world.EclipseLink.entity' under the src (Source) package. Under the given package, create a class with any name, for example, student.java.

Persistence.xml

This module performs a vital role in the concept of JPA. Register the database and define the entity class in the XML file.