Spring Data JPA Tutorial: Getting the Required Dependencies

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.

Additional Reading:

If you are not familiar with Spring Data JPA, you should read the following blog post before you continue reading this blog post:

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:

  1. We can manage our dependencies by using the Spring IO Platform.
  2. 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:

  1. Enable the Spring IO Platform.
  2. 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>
Our example application has a few other dependencies as well. You can get the full list of dependencies by reading its pom.xml file.

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 used the dependency versions that are provided by the Spring IO platform. If you want to use newer versions, you can find the latest available versions from mvnrepository.com.

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.

If you want to learn how to use Spring Data JPA, you should read my Spring Data JPA tutorial.
26 comments… add one
  • Tom Dec 10, 2014 @ 12:32

    Thanks for all the tutes, eagerly awaiting the next one. Have also added you on twitter.

  • Naveen Mar 11, 2015 @ 14:48

    Dependencies reference is pretty handy ... thanks :)

    • Petri Mar 11, 2015 @ 19:44

      You are welcome!

  • Anonymous Jun 27, 2015 @ 14:30

    Really nice tutorial, but I wish if you can show us how to setup the project in eclipse or STS.

    • Petri Jun 28, 2015 @ 10:31

      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?

  • Matt Moran Jul 15, 2015 @ 14:00

    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 :-)

    • Petri Jul 15, 2015 @ 14:23

      Great! I am happy to hear that you were able to solve your problem.

  • Ken Oct 6, 2015 @ 14:26

    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)

    • Petri Oct 6, 2015 @ 15:55

      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?

  • david zornosa Sep 2, 2016 @ 19:54

    hi, i enjoyed reading your articles. great style. enough info without being cumbersome or hard to read, well organized. keep up the good job

    • Petri Sep 5, 2016 @ 21:14

      Thank you for your kind words. I really appreciate them.

  • pralad Mar 1, 2017 @ 19:24

    There is many dependency in your github example pages , isn't it overhead to use all of them or Do i need all ?

    • Petri Mar 4, 2017 @ 11:44

      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.

  • Shane Dec 15, 2017 @ 19:02

    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

    • Petri Dec 19, 2017 @ 19:51

      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).

  • arun singh Feb 22, 2018 @ 14:06

    big help, thanks

    • Petri Feb 22, 2018 @ 17:56

      You are welcome!

  • Viraj Mar 20, 2018 @ 7:47

    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

    • Petri Mar 20, 2018 @ 18:49

      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.

  • Rineez Jul 20, 2020 @ 13:57

    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.

    • Petri Jul 21, 2020 @ 0:02

      Thank you for pointing this out. It should be fixed now.

  • CS Oct 15, 2022 @ 10:27

    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

    • Petri Oct 21, 2022 @ 22:33

      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.

Leave a Reply