JPA stands for Java Persistence API , is just a specification that facilitates object-relational mapping to manage relational data in Java applications. It provides a platform to work directly with objects instead of using SQL statements. The Java Persistence API (JPA) is a specification of Java. It is used to persist data between Java object and relational database. JPA acts as a bridge between object-oriented domain models and relational database systems. As JPA is just a specification, it doesn't perform any operation by itself. It requires an implementation. So, ORM tools like Hibernate, TopLink and iBatis implements JPA specifications for data persistence.
The Java Persistence API (JPA), in 2019 renamed to Jakarta Persistence, 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/Jakarta EE. Persistence in this context covers three areas: The reference implementation for JPA is EclipseLink.
The final release date of the JPA 1.0 specification was 11 May 2006 as part of Java Community Process JSR 220.
The JPA 2.0 specification was released 10 December 2009 (The Java EE 6 platform requires JPA 2.0)
The JPA 2.1 specification was released 22 April 2013 (The Java EE 7 platform requires JPA 2.1[2].)
A persistence entity is a lightweight Java class whose state is typically persisted to a table in a relational database. Instances of such an entity correspond to individual rows in the table. Entities typically have relationships with other entities, and these relationships are expressed through object/relational metadata. Object/relational metadata can be specified directly in the entity class file by using annotations, or in a separate XML descriptor file distributed with the application.
The Java Persistence Query Language (JPQL) makes queries against entities stored in a relational database. Queries resemble SQL queries in syntax, but operate against entity objects rather than directly with database tables.
The EJB 3.0 specification (itself part of the Java EE 5 platform) included a definition of the Java Persistence API. However, end-users do not need an EJB container or a Java EE application server in order to run applications that use this persistence API.[3] Future versions of the Java Persistence API will be defined in a separate JSR and specification rather than in the EJB JSR/specification. The Java Persistence API replaces the persistence solution of EJB 2.0 CMP (Container Managed Persistence).
The Java Persistence API was developed in part to unify the Java Data Objects API, and the EJB 2.0 Container Managed Persistence (CMP) API. (As of 2009) most products supporting each of those APIs support the Java Persistence API.
The Java Persistence API specifies persistence only for relational database management systems. That is, JPA focuses on object-relational mapping (ORM) (note that there are JPA providers who support other database models besides relational database, but this is outside the scope of what JPA was designed for). Refer to JPA 2 spec section 1 introduction for clarification of the role of JPA, which states very clearly "The technical objective of this work is to provide an object/relational mapping facility for the Java application developer using a Java domain model to manage a relational database."
The Java Data Objects specification supports ORM, as well as persistence to other types of database models, for example flat file databases and NoSQL databases, including document databases, graph databases, as well as literally any other conceivable datastore.
The designers of the Java Persistence API aimed to provide for relational persistence, with many of the key areas taken from object-relational mapping tools such as Hibernate and TopLink. Java Persistence API improved on and replaced EJB 2.0, evidenced by its inclusion in EJB 3.0. The Service Data Objects (SDO) API (JSR 235) has a very different objective to the Java Persistence API and is considered complementary. The SDO API is designed for service-oriented architectures, multiple data formats rather than only relational data, and multiple programming languages. The Java Community Process manages the Java version of the SDO API; the C++ version of the SDO API is managed via OASIS.
Hibernate provides an open source object-relational mapping framework for Java. Versions 3.2 and later provide an implementation for the Java Persistence API. Gavin King founded the Hibernate project. He represented JBoss on JSR 220, the JCP expert group charged with developing JPA. This led to ongoing controversy and speculation surrounding the relationship between JPA and Hibernate. Sun Microsystems has stated that ideas came from several frameworks, including Hibernate and Java Data Objects.
An implementation of the repository abstraction that's a key building block of Domain-Driven Design based on the Java application framework Spring. Transparently supports all available JPA implementations and supports CRUD operations as well as the convenient execution of database queries.
Object Relational Mapping (ORM) is a functionality which is used to develop and maintain a relationship between an object and relational database by mapping an object state to database column. It is capable to handle various database operations easily such as inserting, updating, deleting, find, fetch all.
Following are the various frameworks that function on ORM mechanism :
1. Hibernate
2. TopLink
3. ORMLite
4. iBATIS
5. JPOX
Mapping Directions are divided into two parts :
Unidirectional relationship : In this relationship, only one entity can refer the properties to another. It contains only one owing side that specifies how an update can be made in the database.
Bidirectional relationship : This relationship contains an owning side as well as an inverse side. So here every entity has a relationship field or refer the property to other entity.
Following are the various ORM mappings :
One-to-one : This association is represented by @OneToOne annotation. Here, instance of each entity is related to a single instance of another entity.
One-to-many: This association is represented by @OneToMany annotation. In this relationship, an instance of one entity can be related to more than one instance of another entity.
Many-to-one : This mapping is defined by @ManyToOne annotation. In this relationship, multiple instances of an entity can be related to single instance of another entity.
Many-to-many : This association is represented by @ManyToMany annotation. Here, multiple instances of an entity can be related to multiple instances of another entity. In this mapping, any side can be the owing side.
JDBC Save and saveAndFlush both can be used for saving entities. They both are both belong to the Spring data library. save may or may not write your changes to the DB straight away. When we call saveAndFlush system are enforcing the synchronization of your model state with the DB.
| Key | Save | SaveAndFlush |
|---|---|---|
| Repository | It belongs to CrudRepository | It belongs to JPARepository |
| Data flush Strategy | It doesn't flush data directly to a database until and unless we explicitly call flush and commit method. | It's flush directly flush data to a database. |
| Bulk Save | CrudRepository provides bulk save method | saveAndFlush method doesn't support the bulk operation |
| Data Visibility after saving | It doesn't flush data directly to a database, therefore, changes will not be visible outside the transaction unless we explicitly call commit() in this transaction. | Changes will be visible outside the transaction also. |
| Use Case | We use this method when we don't need to use the saved changes at a later point in the same transaction. | We use this method when we need to use the saved changes at a later point in the same transaction. |
| JPA | Spring Data JPA |
|---|---|
| JPA is a specification of Object Relational Mapper in Java. | Spring Data JPA is a simplified abstraction over JPA. |
| JPA can also manage transactions and is built on top of JDBC, allowing us to continue using our native database language. | Spring Data JPA adds a layer of abstraction to the JPA and more adaptable than JPA and provides simple repositories. |
| For query language, JPA is JPQL (Java Persistence Query Language) | For query language, Spring Data JPA is JPQL (Java Persistence Query Language) |
| JPA provides a JPA caching implementation technique for enhancing processes. | Spring Data JPA also provides a JPA caching process. |
| JPA is mainly depends on the JPA implementation. | Spring Data JPA also relies on JPA implementations. |
| JPA can work with any JPA-compliant implementation | Spring Data JPA can not work with any JPA-compliant implementation |
| JPA can be integrated with the Spring | Spring Data JPA can or can not be integrated with the Spring Data family. |