Java Persistence API
The Java Persistence API (JPA) is
a Java application programming interface specification that
describes the management of relational data in applications using Java Platform,
Standard Edition and Java Platform, Enterprise Edition.
The Java Persistence API provides a POJO persistence model for
object-relational mapping (ORM).
ORM is a programming ability to covert data from object type to
relational type and vice versa. The main feature of ORM is mapping or binding
an object to its data in the database.
Following architecture explains the flow of storing objects into
any relational database.
Understanding the above architecture:
Module 1: In this module
there will be Object data under POJO classes and business logic under service
interfaces and classes will be implemented. It is the main business component
layer, which has business logic operations and attributes.
For example let us take any table ‘Payment’ from any database:
Payment POJO class contain attributes such as Id, CompanyId, Status,
Payment type etc.
And methods like setter and getter methods of those attributes
and Payment DAO/Service classes contains service methods such as create Payment,
find Payment etc.
Module 2: In this module we
will be creating the bindings for data mapping using the Java persistence
framework.
This module will be holding the persistence.xml (replicating the
ORM config file ORM.xml), JPA loader, JPA provider and Object grid.
1. JPA Provider: Any vendor
product providing the Java Persistence flavor like Eclipselink, Hibernate,
Toplink.
2. Object mapping file: This will be
persistence.xml in case of Eclipselink based implementation or it could be any
ORM.xml file depending on the vendor we have selected.
3. JPA Loader: This acts like a
cache memory which holds relational grid data and works as if a copy of
database to interact with Service classes for data in POJOs for our
implementation.
4. Object Grid: It is a temporary
location holding data before all queries hit the database it is first effected
on the data in the object grid. Only after it is committed, it effects the main
database.
Module 3: This is the last
phase where object – data mapping actually comes into action and deals with the
database for Select / Insert / Update / Delete data.
Only when the business component commit the data, it is stored
into the database physically. Until then the modified data is stored in a cache
memory as a grid format. Same is the process for obtaining data.
There are following areas which can be covered in Java
Persistence
·
The Java Persistence API
·
The query language
·
The Java Persistence Criteria API
·
Object/relational mapping metadata
Where to use JPA?
To reduce the burden of writing codes for relational object
management, a programmer follows the ‘JPA Provider’ framework. It brings
following benefits along with its ORM framework / implementation:
·
Idiomatic persistence: It enables you to write the persistence
classes using object oriented classes.
·
High Performance: It has many fetching techniques and hopeful
locking techniques.
·
Reliable: It is highly stable and eminent. Used by many
industrial programmers.
Entities
An entity is a lightweight persistence domain object. Typically
an entity represents a table in a relational database, and each entity instance
corresponds to a row in that table.
An entity class must follow these requirements.
·
The class must be annotated with the javax.persistence.Entity
annotation.
·
The class must have a public or protected, no-argument
constructor. The class may have other constructors.
·
The class must not be declared final. No methods or persistent
instance variables must be declared final.
·
Entities may extend both entity and
non-entity classes, and non-entity classes may extend entity classes.
·
The persistent state of an entity can
be accessed through either the entity’s instance variables or properties. The
fields or properties must be of the following Java language types:
§ Java primitive types
§ java.lang.String
§ Other serializable types, including:
§ Wrappers of Java primitive types
§ java.math.BigInteger
§ java.math.BigDecimal
§ java.util.Date
§ java.util.Calendar
§ java.sql.Date
§ java.sql.Time
§ java.sql.TimeStamp
§ User-defined serializable types
§ byte[]
§ Byte[]
§ char[]
§ Character[]
·
If the entity class uses persistent
fields, the Persistence runtime accesses entity-class instance variables
directly. All fields not annotated javax.persistence.Transient or not marked as
Java transient will be persisted to the data store.
·
Primary Keys in Entities
Each entity has a unique object identifier. A customer
entity, for example, might be identified by a customer number. The unique
identifier, or primary key, enables clients to locate a particular entity
instance. Every entity must have a primary key. An entity may have either a
simple or a composite primary key.
Simple primary keys use the javax.persistence.Id
annotation to denote the primary key property or field.
Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking.
ReplyDeleteHibernate Training in Chennai
Spring and Hibernate Training in Chennai
Hibernate Training in OMR
Spring Training in Chennai
Spring framework Training in Chennai
soft skills training in chennai
core java training in chennai
Hibernate Training in Tambaram