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.

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.

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.

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.

My "Test With Spring" course helps you to write unit, integration, and end-to-end tests for Spring and Spring Boot Web Apps: CHECK IT OUT >>

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>

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.

My "Test With Spring" course helps you to write unit, integration, and end-to-end tests for Spring and Spring Boot Web Apps: 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.