JPA – Java Persistence API is the official java specification to work with an ORM tool. Hibernate is the most popular ORM tool out there which implements the JPA standard. As of August 2019, JPA 2.2 is the latest specification standard, also known as JSR 338. In this introduction to JPA and Hibernate tutorial, you will learn their basics and advantages of using them.

This course requires a good knowledge of Apache Maven, please follow the Maven tutorial if you are new to the Java ecosystem.

What is Persistence?

Today’s application requires persistent data, meaning the data needs to be reliably stored in a database. Then the application should be able to retrieve the data from the database and convert it into java objects whenever needed. As you know, there are several databases today like SQL databases, NoSQL databases, time-series databases and so on. This tutorial will only be talking about the SQL databases (relational database) like MySQL, Oracle Database, MS SQL, H2 and etc.

Object persistence means, individual objects living in the application memory (RAM), can be saved in a data store and be recreated later as and when needed.

You must have worked on at least one relational database (RDBMS). There are always challenges when you want to use RDBMS in Java. This is because they both work in a different paradigm. A simple reason, for example, Java (any OOPs language) uses associations and inheritance, but RDBMS uses foreign key constraint. This makes you write extra codes to take care of Inheritance, Object navigation, associations, checking the object’s equality and so on.

What is ORM?

An ORM (Object-relational mapping) is a tool that simplifies the Object creation, manipulation and object access to/from the database. Technically, ORM is a wrapper on top of the JDBC layer that automates a lot of boilerplate code. There are several ORM tools available in Java such as Jboss Hibernate, Oracle TopLink, EclipseLink, OpenJPA.

ORM architecture

ORM tools are used for Relational databases like MySql, Oracle database, db2 and etc. For NoSQL databases, we make use of OGM tools which is out of scope for this tutorial.

What is Hibernate?

Hibernate is an ORM tool which is used in your DAO (data access layer) layer. It makes use of Hibernate API to effectively load, store, update, delete and query, etc its domain objects.

As you can see in the diagram, ORM seats between your database and DA Layer.

Remember, Hibernate is a JPA compliant ORM tool.

What is JPA?

JPA stands for Java Persistent API. It is an official specification from Java that dictates the APIs to be used to persist Entities (Java Objects) in the database. This specification is implemented by Hibernate and other ORM tools.

As mentioned earlier, there are several ORM tools that implement their own API. This becomes a portability problem if you want to switch between them for any reason. This is where JPA helps you by providing an API layer on top of ORM tools. Just like JDBC API vs various database core drivers.

JPA/ORM tools are only for RDBMS, not for schemaless databases.

Advantages of JPA and Hibernate

In this introduction to JPA and hibernate page

  1. Productivity – Eliminates a lot of boilerplate JDBC codes, almost 95% of developers work.
  2. Maintainability – The code becomes more readable and easy to refactor if needed.
  3. Performance – You can avoid commonly made mistakes as well as make use of connection pools in JPA to improve performance.
  4. DB Vendor independence – Hibernate and JPA allows you to move from one RDBMS to another with minimum config changes, and almost no code changes.
  5. ORM vendor independence – Using JPA API strictly, you can easily move from one ORM to another with minimum config changes.

JPA versions history

JPA has gone through 3 major revisions of its draft.

  1. JPA 1.0 was part of JSR 220, which was originally for EJB 3.0, on May 2006 using JSR 220.
  2. JPA 2.0 was introduced as part of JSR 317 on December 2009.
  3. JPA 2.1 was specified in JSR 338 on April 2013.
  4. JPA 2.2 specification was published on July 2017.

This tutorial will be focusing on JPA 2.2 with Hibernate.

This is all as part of the introduction to JPA and Hibernate. Let me know your feedback in the comments below. Continue to the next page to understand the mapping of an Entity in JPA.

By |Last Updated: August 25th, 2019|Categories: Hibernate, JPA|

Table of Contents