In this blog post, you get a simple example of how to implement Spring JPA in your rest API microservice. You will get the beginner's steps from annotations to repository and query methods in this quick guide.
When I first was introduced to the concept of Spring JPA I liked the idea, but I did not know JPA and was not sure exactly how to implement JPA in my rest API microservice, let alone Spring JPA.
I was not sure how to start and how JPA worked. I managed to set it up but I needed a very simple example to implement and use it. So, now I am writing this guide for anyone with that exact issue. I hope you find it useful.
What is JPA exactly?
JPA stands for Java Persistence API.
Put very shortly it is a framework developed to access a database in Java applications.
It is database access made very easy. JPA is object-oriented — you work on objects and classes instead of tables and relations.
Spring JPA takes it one step further and adds an extra layer. This means that the interaction with the database in a sense becomes more business-oriented and not as complex and technical. This is very useful in my everyday work because I do not have to deal with writing long SQL statements. I write methods instead, which is also much easier to read and comprehend.
So, with Spring JPA, you can say goodbye to long SQL statements and boilerplate code in your Java application. Spring JPA offers a range of annotations and an easy way of implementing JPA.
Where to start?
This guide assumes that you are past setting up Spring JPA for your application.
The first step is to make an “entity” for each table you want to access.
I will show you a simple example:
Our example database is a database called “Customer”.
To access this table with Spring JPA, we have to create two new classes.
Spring JPA Entity
First, we create a class in our Java application. Let us call it “CustomerEntity”.
This is a POJO representing the database table.
We add the fields corresponding to the fields in the table.
We add the getters and setters (like we would in any other Java class).
Now, how do we turn this class into something JPA will recognize?
Annotations are the answer!
We add the notation @entity on top of the class definition:
Now, JPA knows that it is a JPA entity.
JPA requires that we add a primary key.
Let us name it “id” and add the required @id annotation.
We add the @Table annotation with an attribute, name, that describes the name of the table in the database:
The last annotation, for now, is @Column that we add above the fields in our class.
They describe the columns in the table:
That’s it! We have now created the entity to represent the table.
Spring JPA Repository
What is the next step?
We need a repository class to represent the SQL queries.
We need a new interface class, CustomerRepository:
As you can see, it extends JpaRepository. That way, we have a lot of JPA functionality at our hands.
Now, we add a method that fetches a customer from an id, and call the method “findCustomerById”:
This method is really a SQL query wrapped into a nice, clean method name!
How nice is this!
There are keywords and rules for creating these methods.
In this example, since we are fetching data, the method name starts with the word “find”.
Next, in the method name, we add the columns we want to look up.
Finally, we use the keyword “by” to define what value we want to look them up by.
I will give you two more examples of what a query method could also look like:
I hope you agree that they are quite self-explanatory and easy to read.
The method names can end up very long, but still, you can read your way out of it to see what they do.
Look here for further explanation on how to create query methods:
Look here for a full list of applicable keywords for putting inside of the query methods:
How to use Spring JPA in your application
The method created in the repository class is now available anywhere in your Java application.
You can call it anywhere, for example like this:
You have to map the result to whatever objects you have in your application.
The guide will not cover this part, nor will it cover any other aspects of Spring JPA.
Get my code example here: https://github.com/anna-knudsen/jpa-spring-simple-example
I have shown you a very simple example of how to create a Spring JPA query.
There is a lot more to Spring JPA, like joins, performance consideration, a concept called entity graphs and so much more.
For now, you have an easy example to start with and a little idea of what Spring JPA is and how to use it.
I hope this will get you started!
By Anna Knudsen
Software Developer, Bankdata
As a software developer at Bankdata, I help make a difference for thousands of bank customers in Denmark.
In my team, we primarily develop API rest services for everything concerning credit cards — on Bankdata’s new Open Shift platform. It is still quite new to me, but I try to learn all the new technologies, that comes with this particular platform and I find the possibilities very exciting. Before working with API rest, I was a Java developer and throughout the years, I have worked a lot with web technologies. I believe that sharing technological knowledge — hence this blog — can make a difference for the future developers and users of digital banking. Besides expanding my knowledge of new technologies, I cherish singing and playing in a band in my spare time as a nice opposite to what I do for a living.