I released the starter package of my Test With Spring course. Take a look at the course >>

Spring Data JPA Tutorial: Getting the Required Dependencies

Four lego blocks

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.

I released the starter package of my "Test With Spring" course:

CHECK IT OUT >>

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.

I released the starter package of my "Test With Spring" course:

CHECK IT OUT >>

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.

About the Author

Petri Kainulainen is passionate about software development and continuous improvement. He is specialized in software development with the Spring Framework and is the author of Spring Data book.

About Petri Kainulainen →

16 comments… add one
  • Thanks for all the tutes, eagerly awaiting the next one. Have also added you on twitter.

    Reply
  • Dependencies reference is pretty handy … thanks :)

    Reply
    • You are welcome!

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

    Reply
    • 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?

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

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

      Reply
  • 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)

    Reply
    • 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?

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

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

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

    Reply
    • 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.

      Reply

Leave a Comment