In this tutorial, we will build a simple Spring boot 2 application with access to a Postgres database using JPA.

 

Background:

 

Before we get started, let us get a couple of definitions out of the way.

 

What is JPA?

JPA stands for the “Java Persistence API”. It is a java specification that defines how data in a relational database can be managed on the Java platform.

 

What is Hibernate?

Hibernate ORM, commonly referred to as simply “Hibernate” is an implementation of the JPA specification. While it was common to directly use the Hibernate API, it is now recommended to use the JPA interface, with Hibernate as the underlying framework. One can think of JPA as the controls in a car and Hibernate as the engine.

 

What is Spring Data?

Spring data is a group of technologies from Spring that provide abstraction and utility frameworks that aim to facilitate data access configuration and reduce boilerplate code. More information can be found here .

Setting up the Spring-boot application:

In our example, we will set up a Spring-boot application with a JPA/Postgres stack. In order to start, let us add the required dependencies. Note that the Spring-boot-starter-parent has been used as the parent project.

 

 

Notice the first two dependencies in our list. The first one is the “spring-boot-starter-data-jpa”. Adding this dependency will automatically pull in the the Java-persistence library, Hibernate-core, Spring-data and the spring-boot-starter-jdbc. The second dependency “postgresql” will add the required Postgres driver and is required to be able to interact with the underlying Postgres database management system. If you are using another DBMS then you will need to add the dependency for the corresponding driver.

 

The next step is to configure the Spring boot application to access the Postgres database management system. To achieve this, we will set have to add the jdbc url, username, password and the hibernate.dll-auto property (more on that later) to the application.properties file. Again, the JDBC url is dependent on the DBMS used. No password was configured on our DB server, therefore it was left blank (don’t do this in a prod environment).

 

 

DDL stands for “Data Definition Language”. It is a programming language used to define database structures and schemas. The value of the spring.jpa.hibernate.ddl-auto property can be set to instruct Hibernate to create our database schema, to update it, validate it, or simply do nothing. In our example, we set the value to “create”. This means that on application start-up, Hibernate will check the data models that we have created, and it will attempt to create a database schema that matches these models. The advantage of doing so is that you do not need to main different sets of database creation scripts. If your application is deployed on different platforms with different database system vendors, then the benefit is greater, as you will not need to create the scripts for the different vendor systems. Of course this feature can be turned off by setting the value to “none”. In that case, you need to make sure that your database schema exists with the required database tables. More information about this setting can be found here

 

Setting up our first entity:

 

Now that we got the application configuration out of the way, it is time to create our first persistence entity. In our example, we will create a “Bank account” entity. We will expand on that example in later articles. For now, our bank account entity will look as follows:

 

 

Let us take a look at some of the annotations used in this class:

 

@Entity: Classes annotated with @Entity are considered persistent entity definitions. Usually this means that a class maps to a database table. This annotation is required as Hibernate scans model packages for classes with this annotation.

@Id: Specifies that the annotated variable will be considered the primary key of the entity.

@GeneratedValue: This annotation indicates that the given value is a generated one. In other words, the user should not set this value explicitly. It will be generated automatically. There are multiple strategies for generating primary keys, such as using values from a sequence, use an underlying table or letting the database decide how the values are created. For simplicity, we use “AUTO”, which means that Hibernate will decide how to generate the keys.

@Version: The version annotation indicates the version number of the given entity. Every time an instance of an entity is modified, the version of that instance is incremented. Version numbers start from 0.

 

Setting up the repository:

 

Gone are the days where DAO’s were needed to be written and implemented, one interface, class and method at a time in order to have a way to access and manage data of a specific entity. With the Spring-Data CrudRepository interface, data access object implementation has been greatly simplified. All that is needed is to create an interface that implements the “CrudRepository” interface, and you are done.

 

 

The BankAccountRepository simply needs to extend the CrudRepository interface, with the BankAccount class and Long ( the primary key class) as the typed parameters. By default, the CrudRepository interface provides convinience methods such as save, saveAll, findById, findall, delete, deleteAll, etc..  What if we need extra functionality, such as searching by the bank account number?. In this case, we simply needs to create a method with the name “findByAccountNumber”. Since we use the same variable name as defined in the entity class, Spring data can automatically figure out the search parameter and the search query.

 

Trying it out:

 

Let us put everything together by trying out the Entity and the corresponding repository that we just created. For this, we will create a “CommandLineRunner” bean. This bean will run once we start up our Spring-boot application and it is a useful development feature when the whole application context needs to be started up and tested for a specific procedure. We will obtain the bank repository from the application context and attempt to perform some save/search operations.

 

In our example, we created two bank accounts. Each bank account is saved using the repository’s save method. We will then use the find all method to get all the accounts in the database using the findall method. The final step is to use our additional search method by searching for a bank account using the account number. If we run the program about, we will get the following output:

 

As you can see, the repository was able to find the bank account using the account number successfully. The findall method was also able to find all our entities after they were saved. Note that if your application has multiple packages with JPA annotated entities, then Spring boot will automatically scan them. If you would like to scan specific packages only, then you can use the @EntityScan annotation.

 

 

Now, let us take a quick look on the database side. We will see that Hibernate has automatically created our schema, and our data has been persisted.

 

 

 

 

Summary:

In this tutorial, we explored how to get a Spring boot application with a JPA backend up and running. We added the required Spring JPA dependency and the required DBMS driver to our POM.xml. Then we added the required configurations to the application.properties file. We defined the persistence entity using JPA annotations and we created a Repository for accessing the data of that entity. Finally, we were able to test the application using a CommandLineRunner bean and we were able to confirm that the application was running as intended.

If you would like to have a solid knowledge foundation about JPA and Hibernate, then I strongly recommend reading the book “High-Performance Java Persistence” by Vlad Mihalcea. The book focuses on JPA and working with relational databases in order to cleanly develop applications that utilize a persistence backend. The author is one of the developers of the Hibernate framework which gives it a high authority on the topic.

You can check more details about the book, how other users rate the book and the price from our Amazon affiliate link (redirects to your local Amazon store): “High-Performance Java Persistence