JPA – Java Persistence API Tutorial – Introduction

jpa - Java Persistence API Tutorial

Java Persistence API or simply JPA is one of the many Java persistence technologies, providing Object graph persistence to relational databases through Object-Relational Mapping. It strives to solve object/relational paradigm mismatch problems with other benefits like creating database independent queries.

Examples of other Java persistence technologies includes but not limited to JDBC, Serialization and JDO.

If you are new to Java Persistence, please visit our Main Article: Java Persistence.

What is JPA?

According to the official definition:

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


Note: Java Persistence API is a technology whose specification is provided by Java Committe. To use it in your application, you have to use it via. one of it’s implementation framework. You can not use it directly.

If you see some code which looks confusing and makes you think that it is using plain Java Persistence API, go for the configuration file. You shall find there one of the name of many JPA implementations out there.

So, we have graph of objects on one side, a JPA implementation like Hibernate in between and a relational database on other side.

With the help of JPA implementation, we store these graph of objects in a relational database.

In a Layered Architecture, this is how Java Persistence API interacts in Persistence Layer:

JPA in Layered Architecture
JPA in Layered Architecture

Business layer calls persistence layer classes. Now, these persistence classes stores and retrieves data through Java Persistence API, with the help of Entity classes.

Below image gives a basic example of mapping of multiple Employee objects into an Employee table,

Here is an Entity class.

JPA ORM Mapping

What would have been the scenario without JPA?

Supposing there is nothing like JPA, we would be persisting the data with the help of one of other Java persistence technologies like JDBC.

For the scenario above, we would be creating two insert SQL queries and executing them one by one with the help of JDBC statements.

One may say that, this does not seems to be such a big issue. This issue is not worth the effort creating a separate framework.

But remember, the above scenario is one of the most basic examples. It does not have any object/relational paradigm mismatch problems which JPA specifically tries to solve.

What does JPA provides?

In general, JPA provides below facilities:

  • Object/Relational Mapping : Developers need not talk in terms of database queries. They can just use functions like save(objects) , Objects <- find (Criteria) and that’s it. All low level queries and data mapping is done by JPA.
  • Database independence Queries: With the help of Java Persistence API Query Language, you can create portable queries across databases.

 Pre-requisites to learn JPA

To understand the basic concepts of JPA, a person requires only two things:

  1. Understanding of OOPS: What are objects and how one object is related to another etc.
  2. Understanding of SQL/JDBC. JPA learning does not require you to create complex queries for a particular problem. But you should know the meanings of SQL clauses and functions.

If you have the above two skills, understanding Java Persistence API is not an issue,

For advance JPA technicalities, if a person is acquainted with some Professional Java programming, it will be easy to grasp.

For beginners, suggestion is to go slow and understand each concept carefully. Then it’s smooth.

Ankush Pahwa is an IT Majdoor (Labourer) in Indian IT industry for last 5 years. Don’t know what else to say, so stopping here.

Self-Learning -> Focused Group -> Learning Intensification -> Self-Learning

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *

JPA Tutorial

jpa - Java Persistence API Tutorial

JPA Concepts

LinkedIn Auto Publish Powered By :