Introduction to the Java Persistence API


Overview

The Java Persistence API (JPA) is the Java standard for mapping Java objects to a relational database. Mapping Java objects to database tables and vice versa is called Object-relational mapping (ORM). The Java Persistence API (JPA) is one possible approach to ORM. Via JPA the developer can map, store, update and retrieve data from relational databases to Java objects and vice versa. JPA can be used in Java-EE and Java-SE applications.
JPA defines only specifications, it does not provide an implementation.
JPA implementation is provided as a reference implementation by the vendors developing O/R Mapper such as Hibernate, EclipseLink and Apache OpenJPA.
JPA permits the developer to work directly with objects rather than with SQL statements. The JPA implementation is typically called persistence provider.

JPA Providers

JPA is an open source API, therefore various enterprise vendors such as Oracle, Redhat, Eclipse, etc. provide new products by adding the JPA persistence flavor in them. Some of these products include:
Hibernate, EclipseLink, TopLink, Spring Data JPA, etc.

What is ORM?

The term Object/Relational Mapping refers to the technique of mapping data from an object model representation to a relational data model representation (and vice versa).
For example, let's look at below diagram shows mapping between Student java model and database relational student table.
Hibernate is the most popular ORM framework and implementation of JPA. You can learn complete hibernate framework at Hibernate Tutorial

Package javax.persistence

The Java Persistence API provides Java developers with an object/relational mapping facility for managing relational data in Java applications. 

JPA API Versions

The first version of Java Persistence API, JPA 1.0 was released in 2006 as a part of EJB 3.0 specification.
Following are the other development versions released under JPA specification: -
1. JPA 2.0 - This version was released in the last of 2009. Following are the important features of this version: -
  • It supports validation
  • It expands the functionality of object-relational mapping
  • It shares the object of cache support
2. JPA 2.1 - The JPA 2.1 was released in 2013 with the following features: -
  • It allows the fetching of objects
  • It provides support for criteria update/delete
  • It generates a schema
3. JPA 2.2 - The JPA 2.2 was released in 2017. Some of its important features are: -
  • It supports Java 8 Date and Time
  • It provides @Repeatable annotation that can be used when we want to apply the same annotations to a declaration or type use
  • It allows JPA annotation to be used in meta-annotations
  • It provides an ability to stream a query result

What are the advantages of JPA?

The advantages of JPA are given below.
  • The burden of interacting with the database reduces significantly by using JPA
  • The user programming becomes easy by concealing the O/R mapping and database access processing
  • The cost of creating the definition file is reduced by using annotations
  • We can merge the applications used with other JPA providers
  • Using different implementations can add the features to the standard Implementation which can later be the part of JPA specification

Next JPA articles

  • JPA Entity Class Basics - Check out how to create JPA entity, what are rules to create JPA entity and what are features JPA Entity provides like cascading, lazy, relationships, inheritance etc
  • JPA Architecture - In this article, we will discuss the architecture(core classes and interfaces of Java Persistence API) of the JPA specification
You can learn complete JPA at JPA Tutorial - Java Persistence API

Comments