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:
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 Employee.java is an Entity class.
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:
- Understanding of OOPS: What are objects and how one object is related to another etc.
- 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.