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:

springdatajpalayers

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:

springdatajrepositories

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.

Summary

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 learn how to use Spring Data JPA, you should read my Spring Data JPA tutorial.
49 comments… add one
  • Peter Dec 17, 2014 @ 12:40

    Many Thanks!

    • Petri Dec 17, 2014 @ 19:46

      You are welcome!

  • Krisna Feb 6, 2015 @ 5:43

    Nice Post.. Mr Petri

  • vamshi Feb 20, 2015 @ 12:55

    Excellent post

    • Petri Feb 20, 2015 @ 21:41

      Thank you! I am happy to hear that you liked this blog post.

  • Carlos Romero Sep 1, 2015 @ 18:48

    Keep all your jobs in Spring, they are very good, excellent

    Thanks for all Petri.

    Regards

    • Petri Sep 1, 2015 @ 19:26

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

  • Michael Calcagni Dec 18, 2015 @ 0:31

    Enjoyable reading and very helpful in my understanding of data persistence and web app flow.
    Thanks

    • Petri Dec 18, 2015 @ 20:09

      Hi Michael,

      you are welcome. I am happy to hear that this blog post was useful to you.

  • Vasu Feb 19, 2016 @ 16:48

    Hi Petri ,
    It's very easy and nice to read,

    Thanks
    Vasu

    • Petri Feb 19, 2016 @ 21:47

      Hi Vasu,

      You are welcome. I am happy to hear that blog post was useful to you.

  • Soumadeep Feb 22, 2016 @ 10:10

    Can you elaborate n+1 select problem in jpa in detail.

    • Petri Feb 22, 2016 @ 22:01

      Check out this blog post.

      • Soumadeep Feb 23, 2016 @ 7:27

        Hi Petri,
        Firstly thanks for the link and the blog.
        I am eager to know what is the root cause behind n+1 select problem?
        Because in my application for some specific reference in an entity are getting n+1 select problem,not for all the entity.

        • Petri Feb 23, 2016 @ 21:20

          Unfortunately it's impossible to answer to that question because I don't know the answers to the following questions:

          • Which JPA provider are you using?
          • What kind of a query are you using?
          • What kind of a domain model do you have?

          However, the most common cause of the N+1 select problem is that you query an entity and iterate the results (a collection) in a lazy fashion.

          Check out this blog post. It should help you to get more information about the typical performance problems which are caused by JPA.

  • Samuel Mar 15, 2016 @ 1:30

    Hi Petri,

    It's been well over a year since you started writing these blogs? Has the Spring Data JPA APIs changed much? I asked because I'm interested in checking your tutorials out.

    Thanks.

    • Petri Mar 15, 2016 @ 9:14

      Hi Samuel,

      As far as I know, there hasn't been any major changes to existing APIs. In other words, my Spring Data JPA tutorial should be up-to-date.

  • Divs May 5, 2016 @ 15:56

    Precise and really helpful. Many Thanks.

    • Petri May 5, 2016 @ 18:04

      You are welcome.

  • Carlos Zegarra Nov 20, 2016 @ 2:07

    Excellent explanation.

    • Petri Nov 21, 2016 @ 22:52

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

  • Haftu Nov 22, 2016 @ 5:23

    What a great post! Thanks for taking the time to shade some light on Spring Data JPA!

    • Petri Nov 22, 2016 @ 10:43

      You are welcome! I am happy to hear that this post was useful to you.

  • Amar Dec 29, 2016 @ 18:03

    Thanks Petri.

    • Amar Dec 29, 2016 @ 18:07

      Which version of Springs?

      • Petri Dec 30, 2016 @ 11:07

        You are welcome. I am happy to hear that this blog post was useful to you.

        This tutorial uses the Spring IO Platform 1.1.2.RELEASE. This means that the examples use Spring Data JPA 1.7.2.RELEASE.

  • Wachirakorn Jan 16, 2017 @ 17:16

    Nice post. Thanks for your sharing.

    • Petri Jan 17, 2017 @ 17:15

      You are welcome!

  • Durgesh Pathak Apr 25, 2017 @ 8:15

    Very Nice Post .i am happy to conect you
    thank you

    • Petri Apr 25, 2017 @ 9:45

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

  • Nico May 21, 2017 @ 10:06

    Thank you very much! Finally i understood Spring
    It was very nice to read and follow
    Thanks! Keep it up

    • Petri May 21, 2017 @ 13:03

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

  • Ayush Jun 11, 2017 @ 16:38

    Thanks a lot!!!

    • Petri Aug 14, 2017 @ 23:00

      You are welcome.

  • Vasanth Aug 11, 2017 @ 17:30

    Wonderful article with simple words and easy to understand the concepts. Good Job.. Love to read & learn more..

    • Petri Aug 14, 2017 @ 23:00

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

  • Hizmarck Oct 19, 2017 @ 19:22

    Excellent explanation, simple and concise!

    • Petri Oct 22, 2017 @ 12:01

      Thank you! I am happy to hear that you liked this blog post.

  • junxin lai Dec 21, 2017 @ 4:01

    Thank you for this,It's very helpful to me!

  • Bill Dec 28, 2017 @ 9:42

    This tutorial is really helpful for me. Thanks for your post. I really appreciate.

    • Petri Dec 29, 2017 @ 21:11

      You are welcome.

  • arun singh Feb 24, 2018 @ 16:12

    thanks, big help

    • Petri Mar 12, 2018 @ 8:43

      You are welcome.

  • Vijay Apr 2, 2019 @ 13:25

    Its a great easy to read n understandable blog post. And you responding to every comment is beyond practicality. Outstanding work petri! Thanks for your efforts.

    • Petri Apr 6, 2019 @ 16:25

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

  • Yusuf SEZER Sep 27, 2020 @ 20:44

    Thank you for Spring Data JPA series, there are many topics that aren't anywhere.

    • Petri Oct 3, 2020 @ 19:27

      You are welcome. I am happy to hear that this series has been useful to you.

Leave a Reply