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 remove some boilerplate code by creating an abstract base repository class that provides CRUD operations for our entities. This means that we can create concrete repositories by extending that base repository class. In other words, we have to write the code that provides CRUD operations only once.
The problem is that this does not eliminate all boilerplate code. We still have write the code that creates our database queries and invokes them. To make matters worse, we have to do this every time when we create a new database query.
This is a waste of time!
Spring Data JPA was created to solve this problem.
Introducing: Spring Data JPA Tutorial
The New Tutorial
This tutorial describes how you can create JPA repositories without writing any boilerplate code, and it consists of the following blog posts
- Spring Data JPA Tutorial: Introduction provides a quick introduction to Spring Data JPA. It describes what Spring Data JPA really is and provides an overview of the Spring Data repository interfaces.
- Spring Data JPA Tutorial: Getting the Required Dependencies describes how you can get the required dependencies.
- Spring Data JPA Tutorial: Configuration helps you to configure the persistence layer of a Spring application that uses Spring Data JPA and Hibernate.
- Spring Data JPA Tutorial: CRUD describes how you can create a Spring Data JPA repository that provides CRUD operations for an entity.
- Spring Data JPA Tutorial: Introduction to Query Methods gives a very short introduction to query methods. It also describes what kind of values you can return from your query methods and how you can pass method parameters to your query methods.
- Spring Data JPA Tutorial: Creating Database Queries From Method Names describes how you can create database queries from the method names of your query methods.
- Spring Data JPA Tutorial: Creating Database Queries With the @Query Annotation describes how you can create database queries by annotating your query methods with the @Query annotation.
- Spring Data JPA Tutorial: Creating Database Queries With Named Queries describes how you can create database queries by using named queries.
- Spring Data JPA Tutorial: Creating Database Queries With the JPA Criteria API describes how you can create dynamic queries by using the JPA Criteria API.
- Spring Data JPA Tutorial: Creating Database Queries With Querydsl describes how you can create dynamic database queries by using Querydsl.
- Spring Data JPA Tutorial: Sorting describes how you can sort your query results.
- Spring Data JPA Tutorial: Auditing, Part One describes how you can add the creation and modification time fields into your entities by using the auditing infrastructure of Spring Data JPA.
- Spring Data JPA Tutorial: Auditing, Part Two describes how you can add the information of the authenticated user, who created and/or updated an entity, into your entities by using the auditing infrastructure of Spring Data JPA.
The Old Tutorial
This nine-part tutorial will help you to get started with Spring Data JPA. This tutorial is based on Spring Data JPA 1.0.2 but its information is also valid for newer versions of Spring Data JPA. On the other hand, if you are interested of reading about Spring Data JPA 1.2.0, you can start by taking a look at my Spring Data book.
Lets move on and find out what you can learn by reading this tutorial.
- Spring Data JPA Tutorial, Part Three: Custom Queries with Query Methods describes how you can create custom queries by adding query methods to your repository interfaces.
- Spring Data JPA Tutorial, Part Seven: Pagination helps you to paginate your query results.
- Spring Data JPA Tutorial, Part Eight: Adding Functionality to a Repository describes how you can add custom functions to a single repository.
- Spring Data JPA Tutorial, Part Nine: Conclusions evaluates the usefulness of Spring Data JPA, gives recommendations about its usage and provides links to useful resources.
- Spring Data JPA Tutorial: Integration Testing describes how you can write integration tests for your Spring Data JPA repositories.
- Spring Data JPA Tutorial: Using Querydsl in a Multi-module Maven Project describes how you can use Querydsl and Spring Data JPA in a multi-module Maven project.
Congratulations. If you read every part of this tutorial, you know that implementing data access code does not have to be cumbersome. It can be fast, simple and fun.
It is time to put your new skills to use.
- My Spring Data book helps you to create JPA repositories without any boilerplate code and tweak the performance of your applications with Spring Data Redis.
- Spring Data Solr Tutorial describes how you can implement fast search functions with Spring Data Solr.
- Using jOOQ with Spring helps you to get back in control of your SQL. If you are tired of solving ORM related performance problems, you should do yourself a favor and read this tutorial.
This section showcases useful material created by other developers. If you have written a blog post or recorded a video about Spring Data JPA, and want to include it to this section, ping me on Twitter and I will check it out. If I think that it is useful, I will add it to this section.
Java Persistence API
Reference Manuals and Official Guides
- Dynamic, typesafe queries in JPA 2.0
- JPA Criteria API by samples – Part-I
- JPA Criteria API by samples – Part-II
- JPA 2 Criteria API Tutorial
- JPA Pagination
- JPA: Embedding Embeddables within Entities
- JPA: One to One Uni/Bi Directional Relationship Tutorial
- JPA: One to Many Relationship
- JPA: Mapping a Many To Many Relationship
- JPA: Self Referencing Relationships
- JPA: Inheritance with @MappedSuperclass
- Inheritance with @Inheritance
- JPA: @Inheritance with Table Per Class
- JPQL: The Basics of the Java Persistence Query Language
- JPA / JPQL: Intermediate Queries with @NamedQuery
- Criteria API Introduction
Spring Data JPA
Reference Manuals and Official Guides
- Spring Data JPA Reference Manual
- Spring Data JPA API
- Spring Data REST Reference Manual
- Spring Data REST API
- Getting started with Spring Data JPA
- Advanced Spring Data JPA – Specifications and Querydsl
- Accessing Data with JPA
- Accessing JPA Data with REST
- Distributed transactions with multiple databases, Spring Boot, Spring Data JPA and Atomikos
- Spring Data repositories with multiple databases
- The Persistence Layer with Spring Data JPA
- Spring JPA Data + Hibernate + MySQL + MAVEN
- Spring Data JPA with QueryDSL: Repositories made easy
- Spring Data REST in Action
- Spring MVC 3.2 with Spring Data REST (part 1)
- Spring MVC 3.2 with Spring Data REST (part 2)
- Spring boot and spring data jpa tutorial (part 1 / 2)
- Spring boot and spring data jpa tutorial (part 2 / 2)
- An Introduction to Spring Data
- Spring Data Repositories – A Deep Dive
- An Introduction to Spring Data JPA
- Spring Data JPA Configuration Tutorial
- Configuring Spring Data with a MySQL Database
- Spring Data JPA: Defining Query Methods on Repositories
- Spring Data REST: Easily export JPA entities directly to the web
- Spring Data Repositories – Best Practices
- Integration Testing of Spring Data JPA Repositories