Enterprise Java

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

  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.0.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:

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.182</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.7.Final</version>
</dependency>
 
<!-- Spring Data JPA -->
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
    <version>1.7.1.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.

Petri Kainulainen

Petri 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.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Brett Wooldridge
9 years ago

Nice article. Advise upgrading HikariCP dependency to at least 2.2.5 though.

Petri Kainulainen
9 years ago

Hi Brett,

Thank you for comment! I have updated the HikariCP version to the version that is published on my website. The reason why I used the version 1.4.0 is that the Spring IO Platform 1.0.3 uses HikariCP 1.4.0.
This was fixed when the Spring IO Platform 1.1.0 was released. It uses HikariCP 2.2.5.

I will contact Java Code Geeks and hopefully they will update their copy of my blog post as well.

Back to top button