Getting started with JPA & Hibernate

Introduction

This tutorial intends to enable readers to quickly setup hibernate on their local machine and create a sample “Hello world” program where user persists the “Hello world” greeting in the database using hibernate.

We will be using PostgreSQL as the DB, Eclipse Neon as the IDE, Maven as the build tool and Hibernate EntityManager 5.0.1. We will not be using native Hibernate, we will be using Hibernate’s standardised JPA implementation, instead.

What is JPA?
The Java Persistence API (JPA) is a Java API specification that describes the management of relational data in applications using  Java SE and Java EE.

What’s the difference between native hibernate and Hibernate JPA implementation?
Native Hibernate is like the superclass to the standard JPA implementation of Hibernate. Although, a lot has been standardised but there might still be things you can do in native hibernate only. So, this might seem like a bad idea to use JPA implementation but the downside of native implementation is that it will tie your software to Hibernate, whereas, if you use standard JPA implementation you will have the flexibility to replace Hibernate with any other JPA implementation anytime you like.

So, in this tutorial we will be using EntityManagerFactory and EntityManager which are defined by the JPA standard instead of SessionFactory and Session, which are hibernate specific. The EntityManager invokes the hibernate session under the hood, though. If you must need some specific features that are not available in the EntityManager, you can obtain the session by calling:


What are other implementations of JPA?
There are many. DataNucleus (formerly JPOX), EclipseLink (formerly Oracle TopLink) to name a few.

Now, since we understand what we are doing and why we are doing it, let’s get down to business.

Maven Project in Eclipse

Before continuing on with hibernate setup, we need a Maven project ready in eclipse. If you don’t know how to do that, read Creating a Maven project in Eclipse first and then come back. It is a 5 minute read and you will be able to have a working Maven project setup.

With the working maven project at hand, we are now off to the real stuff i.e, setting up Hibernate so that we could test out our learning.

Hibernate setup

In this section we will see, step-by-step the changes we need to make

pom.xml

In your pom.xml, paste Hibernate and postgres dependencies

 

persistence.xml

The standard configuration file for persistence units is located on the classpath in META-INF/persistence.xml. In our case, we don’t have any META-INF folder, so we can create one and create a new persistence.xml file inside it.

Since we intend to try out the hibernate features as small standalone JUnit tests. we will create a source folder first and name it src/test/resources and inside this source folder we will create a folder named META-INF which will contain our persistence.xml

After all this, your project structure will look like this:

Now you need to update your persistence.xml with your database settings and any other settings you want to have.

Entity class

Create an entity class in src/main/java. You can update App.java, if you like.

Test Case

You can update AppTest.java to see EntityManager in action.

Run

Right click the test case to run as JUnit test. In the first attempt you will get an exception:  java.lang.IllegalArgumentException: Unknown entity: com.codecramp.jpa.Greeting

It is OK because at this point EntityManager doesn’t know about this entity. To get rid of this exception all you need to do it update your persistence.xml with

Now, you can run the test case again and this time the test should pass.

Output

Open your database and you should be able to see the newly created table named greeting and a row with your greeting in it.