Home » Java » Enterprise Java » Spring Data JPA Tutorial: Introduction

About Petri Kainulainen

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.

Spring Data JPA Tutorial: Introduction

Creating repositories that use the Java Persistence API is a cumbersome process that takes a lot of time and requires a lot of boilerplate code. We can eliminate some boilerplate code by following these steps:

  1. Create an abstract base repository class that provides CRUD operations for entities.
  2. Create the concrete repository class that extends the abstract base repository class.

The problem of this approach is that we still have to write the code that creates our database queries and invokes them. To make matters worse, we have to do this every time when we want to create a new database query. This is a waste of time.

What would you say if I would tell you that we can create JPA repositories without writing any boilerplate code?

The odds are that you might not believe me, but Spring Data JPA helps us to do just that. The website of the Spring Data JPA project states that:

Implementing a data access layer of an application has been cumbersome for quite a while. Too much boilerplate code has to be written to execute simple queries as well as perform pagination, and auditing. Spring Data JPA aims to significantly improve the implementation of data access layers by reducing the effort to the amount that’s actually needed. As a developer you write your repository interfaces, including custom finder methods, and Spring will provide the implementation automatically

This blog post provides an introduction to Spring Data JPA. We will learn what Spring Data JPA really is and take a quick look at the Spring Data repository interfaces.

Let’s get started.

What Spring Data JPA Is?

Spring Data JPA is not a JPA provider. It is a library / framework that adds an extra layer of abstraction on the top of our JPA provider. If we decide to use Spring Data JPA, the repository layer of our application contains three layers that are described in the following:

  • Spring Data JPA provides support for creating JPA repositories by extending the Spring Data repository interfaces.
  • Spring Data Commons provides the infrastructure that is shared by the datastore specific Spring Data projects.
  • The JPA Provider implements the Java Persistence API.

The following figure illustrates the structure of our repository layer:


Additional Reading:

At first it seems that Spring Data JPA makes our application more complicated, and in a way that is true. It does add an additional layer to our repository layer, but at the same time it frees us from writing any boilerplate code.

That sounds like a good tradeoff. Right?

Introduction to Spring Data Repositories

The power of Spring Data JPA lies in the repository abstraction that is provided by the Spring Data Commons project and extended by the datastore specific sub projects.

We can use Spring Data JPA without paying any attention to the actual implementation of the repository abstraction, but we have to be familiar with the Spring Data repository interfaces. These interfaces are described in the following:

First, the Spring Data Commons project provides the following interfaces:

Second, the Spring Data JPA project provides the following interfaces:

The repository hierarchy looks as follows:


That is nice, but how can we use them?

That is a fair question. The next parts of this tutorial will answer to that question, but essentially we have to follow these steps:

  1. Create a repository interface and extend one of the repository interfaces provided by Spring Data.
  2. Add custom query methods to the created repository interface (if we need them that is).
  3. Inject the repository interface to another component and use the implementation that is provided automatically by Spring.

Let’s move on and summarize what we learned from this blog post.


This blog post has taught us two things:

  • Spring Data JPA is not a JPA provider. It simply “hides” the Java Persistence API (and the JPA provider) behind its repository abstraction.
  • Spring Data provides multiple repository interfaces that are used for different purposes.

The next part of this tutorial describes how we can get the required dependencies.

If you want to know more about Spring Data JPA, you should read my Spring Data JPA Tutorial.

Reference: Spring Data JPA Tutorial: Introduction from our JCG partner Petri Kainulainen at the Petri Kainulainen blog.
(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Notify of

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

Inline Feedbacks
View all comments