Book Image

Spring Data

By : Petri Kainulainen
Book Image

Spring Data

By: Petri Kainulainen

Overview of this book

Spring Framework has always had a good support for different data access technologies. However, developers had to use technology-specific APIs, which often led to a situation where a lot of boilerplate code had to be written in order to implement even the simplest operations. Spring Data changed all this. Spring Data makes it easier to implement Spring-powered applications that use cloud-based storage services, NoSQL databases, map-reduce frameworks or relational databases. "Spring Data" is a practical guide that is full of step-by-step instructions and examples which ensure that you can start using the Java Persistence API and Redis in your applications without extra hassle. This book provides a brief introduction to the underlying data storage technologies, gives step-by-step instructions that will help you utilize the discussed technologies in your applications, and provides a solid foundation for expanding your knowledge beyond the concepts described in this book. You will learn an easier way to manage your entities and to create database queries with Spring Data JPA. This book also demonstrates how you can add custom functions to your repositories. You will also learn how to use the Redis key-value store as data storage and to use its other features for enhancing your applications. "Spring Data" includes all the practical instructions and examples that provide you with all the information you need to create JPA repositories with Spring Data JPA and to utilize the performance of Redis in your applications by using Spring Data Redis.
Table of Contents (13 chapters)

Adding custom functionality to all repositories


Sometimes we have to add custom functionality to all repositories. In this section, we will learn how we can do this and create a custom repository method that is used to delete an entity by using its ID.

We can add custom functionality to all repositories by following these steps:

  1. Create a base interface that declares the custom methods.

  2. Implement the created interface.

  3. Create a repository factory bean.

  4. Configure Spring Data JPA to use our repository factory bean.

  5. Create a repository interface.

  6. Implement a service class that uses the custom functionality.

Creating the base repository interface

We start by creating a base repository interface that declares the methods that are available in the actual repositories. We can do this by:

  1. Creating an interface that takes the type of the managed entity and the type of its ID as a type parameter.

  2. Extending both the JpaRepository<T, ID> and QueryDslPredicateExecutor<T> interfaces in our base repository...