Introduction to the Java Persistence API

This article is and introduction to Java API which teaches you the basics of Java Persistence API (JPA 2.1).

Introduction to the Java Persistence API

This tutorial introduces you with the JPA 2.1

In this section we are going to introduce you about the Java Persistence API. The Java Persistence API is Java standard for persisting the Java Objects (Entity) to the relational database. It provides a way to bridge the gap between Java model and relational databases.

In very simple terms you can persist or simply save your Java objects to the relational database without writing the SQL statements. The JPA provides API's is used in your program for this purpose. Java Persistence API depends on the persistence provider (ORM tool) such as Hibernate, iBatis, OpenEJB, JPA Eclipselink etc.. for actually performing the work of persistence.

The Java Persistence API was originally developed as part of JSR 220. The version of JPA is 2.1 which is a part of JEE 7 specification. JPA 2.1 provides many new features such as Stored Procedure support, Batch Updates etc.. which makes it very useful in developing demanding enterprise applications.

JPA can be used outside the EJB container which means you can use the JPA in your Java SE based applications. JPA don't need any JEE container to work. It can work outside the JEE Container. You can use the JPA based applications on the Tomcat 7 Server also.

JPA is database and JPA implementation independent. Your will be able to switch your persistence provider and also the database server. It is advisable not to use the database specific query while creating your applications.

The Java EE 7 platform needs JPA 2.1 for persistence. The Java Persistence API consists of:

  • The Java Persistence API - Provides the interface for persisting, querying, updating and deleting the entity
  • The query language - This is used to fetch the entity from the relational database.
  • Object/relational mapping metadata - Provides the way to provide the Object/relational metadata in the form of annotations or xml.
  • The Java Persistence Criteria API - The Java Persistence Criteria API is used for fetching the data based on certain conditions.
Advertisement

History

The first version of JPA 1.0 was released on 11 May 2006 as a part of effort made by team of JSR 220. Then JPA 2.0 specification was released on 10th December 2009. The current version of JPA 2.1 specification is part of JEE 7 platform and was released on 22-April-2013. The JPA 2.1 was released with many new features and enables the developers to develop enterprise applications quickly.

Entities

The Entities are simple POJO Java classes which maps to the table of relational database. It is used in Java program and with the help of JPA API 's persisted in the database. The object/relational metadata is used to provide the O/R mapping instruction to the JPA environment. The object/relational metadata can be provided with the help of annotations and xml.

The JPQL or Java Persistence Query Language

The Java Persistence Query Language or simple JPQL is object-relational query language which is defined as a part of JPA Specification. The JPQL is a platform-independent O/R query language. Here name and fields of the entity object is used which internally translated into SQL by the JPA implementation at run time. It saves a lot of development time while creating the persistence layer of a enterprise application. The JPQL is used to SELECT, UPDATE and DELETE operations.

JPA Architecture

The Java Persistence API provides the API for persisting the data into relational database. Since JPA is specification it does not provides any implementation for persisting the objects, application environment must provide third-party persistence provide such as Hibernate. So, you will be able to use free or commercial persistence provider as per your business needs.