Before we can create an application that uses Spring Data JPA, we need to get the required dependencies.
This blog post identifies the required components and describes how we can get them by using Maven.
Let's get started.
If you are not familiar with Spring Data JPA, you should read the following blog post before you continue reading this blog post:
- Spring Data JPA Tutorial: Introduction provides a quick introduction to Spring Data JPA and gives an overview of the Spring Data repository interfaces.
What Components Do We Need?
If we want to implement a persistence layer that uses Spring Data JPA, we need the following components:
- The JDBC driver provides a database specific implementation of the JDBC API. We use the H2 in-memory database because it makes our example application easier to run.
- The datasource provides database connections to our application. We use the HikariCP datasource because it is the fastest datasource on this planet.
- The JPA Provider implements the Java Persistence API. We use Hibernate because it is the most common JPA provider.
- Spring Data JPA hides the used JPA provider behind its repository abstraction.
Let’s move on and find out how we can get the required dependencies with Maven.
Getting the Required Dependencies with Maven
We can get the required dependencies with Maven by using one of these options:
- We can manage our dependencies by using the Spring IO Platform.
- We can manage our dependencies "manually".
Let's take a look at both options.
Using the Spring.IO Platform
If we use the Spring IO Platform, we need to follow these steps:
- Enable the Spring IO Platform.
- Configure the required dependencies in the pom.xml file.
First, we can enable the Spring IO Platform by adding the following XML to our POM file:
<dependencyManagement> <dependencies> <dependency> <groupId>io.spring.platform</groupId> <artifactId>platform-bom</artifactId> <version>1.1.2.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
After we have enabled the Spring IO Platform, we don’t have to worry about dependency versions because the Spring IO Platform takes care of that. This means that we can get the required dependencies by adding the following XML to the dependencies section of our POM file:
<!-- Database (H2) --> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> </dependency> <!-- DataSource (HikariCP) --> <dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> </dependency> <!-- JPA Provider (Hibernate) --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> </dependency> <!-- Spring Data JPA --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> </dependency>
Additional Reading:
Let's move on and find out how we can manage our dependencies manually.
Managing Our Dependencies Manually
If we manage our dependencies "manually", we need specify the version numbers of all dependencies. We can do this by adding the following dependency declarations to the dependencies section of our pom.xml file:
<!-- Database (H2) --> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>1.4.185</version> </dependency> <!-- DataSource (HikariCP) --> <dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>2.2.5</version> </dependency> <!-- JPA Provider (Hibernate) --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>4.3.8.Final</version> </dependency> <!-- Spring Data JPA --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> <version>1.7.2.RELEASE</version> </dependency>
I guess the obvious question is: which option should we use?
What Is the Best Way to Manage Our Dependencies?
If we are starting a new project, we should use the Spring IO Platform because
- We don’t have to worry about the dependency versions. For example, we don’t have to worry about incompatibility issues because we know that our dependencies work together like a charm.
- We can always override the dependency versions provided the by the Spring.IO platform.
On the other hand, if we are adding Spring Data JPA to an existing project, it is often wiser to manage our dependencies manually because it requires less work.
Let's move on and summarize what we learned from this blog post.
Summary
This blog post has taught us four things:
- If we want to implement a persistence layer that uses Spring Data JPA, we need the following components: a JDBC driver, a datasource, a JPA provider, and the Spring Data JPA.
- We can get the required dependencies by using the Spring IO platform or managing our dependencies manually.
- If we are starting a new project, we should use the Spring IO platform because it ensures that our dependencies work together like a charm.
- If we are adding Spring Data JPA to an existing project, we should manage our dependencies manually because it requires less work.
The next part of this tutorial describes how we can configure Spring Data JPA.
P.S. You can the get the example application of this blog post from Github.
Thanks for all the tutes, eagerly awaiting the next one. Have also added you on twitter.
Dependencies reference is pretty handy ... thanks :)
You are welcome!
Really nice tutorial, but I wish if you can show us how to setup the project in eclipse or STS.
I haven't used Eclipse for eight years because its Maven support is not very good. For example, when I downloaded Eclipse Mars and tried to import the example application (File -> Import -> Maven -> Existing Maven Projects), I got the following error:
"No marketplace entries found to handle maven-antrun-plugin:1.7:run in Eclipse. Please see Help for more information."
I tried searching the solution from Google, but most articles / SO questions / web pages talked about the notorious "plugin execution not covered by lifecycle configuration" error. In other words, they did not help me to solve this problem.
This is why I use IntelliJ Idea. It just works.
P.S. If you know a solution to this problem, could you share it with us?
D'oh - problem solved - Eclipse doesn't have the right scope available in its drop-down when you're adding dependency management entries. I put it in manually & hey presto, it works :-)
Great! I am happy to hear that you were able to solve your problem.
Nice tutorial Petri. Everything works until I deploy into Tomcat. Have you run into these kind of problems with hikari? So far I haven't found help from Google:
"The web application [/order] appears to have started a thread named [Hikari Housekeeping Timer (pool HikariPool-0)] but has failed to stop it. This is very likely to create a memory leak."
and a little later:
Could not load java.lang.invoke.LambdaMetafactory.
...
at com.zaxxer.hikari.util.ConcurrentBag.values(ConcurrentBag.java:230)
Hi Ken,
I will investigate this issue when I get back to home (I am currently at work). Are you using Java 7 or Java 8? Also, which Tomcat version are you using?
I wasn't able to reproduce this issue by using the example application of this blog post, Java 8, and Tomcat 8.0.27.
hi, i enjoyed reading your articles. great style. enough info without being cumbersome or hard to read, well organized. keep up the good job
Thank you for your kind words. I really appreciate them.
There is many dependency in your github example pages , isn't it overhead to use all of them or Do i need all ?
Hi,
As you probably noticed, the example applications are web applications and that is why they use a lot of dependencies that aren't related to Spring Data JPA. If you want to write a web application, you might need most of them, but it's impossible to give an exact answer since I don't know what you are trying to do.
Hello,
Thanks for the wonderful tutorial. I am playing with your code (custom-method-all-repos) on Hibernate (5.2.12.Final) and Spring Data (2.0.2.RELEASE). Your code is not working. For example, BaseRepositoryFactoryBean.java has compiler errors. It seems your code is outdated. Do you have any plan to fix this? Where to get the latest code?
Best,
Shane
Hi,
Thank you for reporting this problem. I will take a look at the examples and try to fix them as soon as possible. However, since I am currently busy finishing my testing course, I assume that I can fix this problem in January (2018).
big help, thanks
You are welcome!
Hi,
Thank you very much for informative tutorial. Can you please explain why we need Hikari here. Can't we go forward without adding a Datasource? Sorry , if the question is dumb
Hi,
A
DataSource
is basically a factory that provides connection to the actual data source (database) and you need one if you want to write an application that uses relational databases. I use HikariCP mainly because it's fast and I like its design philosophy (minimalism).If you have any additional questions, don't hesitate to ask them.
Thanks for the post. Looks like the code formatting is messed up for all XML code.
Tags are displayed with > and < instead of angle brackets.
Thank you for pointing this out. It should be fixed now.
Hi Ken,
If Spring Data JPA is kind of a wrapper around Hibernate(JPA provider), why do we need hibernate-entitymanager dependency explicitly in pom.xml?
org.hibernate
hibernate-entitymanager
4.3.8.Final
Hi,
Good question.
It seems that nowadays you don't have to declare the
hibernate-entitymanager
dependency in your POM file because Hibernate is the default JPA provider of Spring Data JPA. In the past you had declare this dependency because you had to explicitly specify the used JPA provider.