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)


Spring Framework has always had good support for different data access technologies. However, one thing remained the same for a long period of time: developers had to implement their data access layer by using technology specific APIs, and often these APIs were not very concise. This led to a situation where one had to write a lot of boilerplate code in order to achieve the desired results. Sounds familiar, right?

The Spring Data project was born as an answer to these problems. Its goal is to provide an easier way to create applications, which use either relational databases or newer data access technologies such as non-relational databases, map-reduce frameworks, or cloud based storage technologies, with the Spring framework. It is essentially a parent project that collects data storage specific subprojects under a single brand. A full list of its subprojects can be found from the homepage of the Spring Data Project:

This book concentrates on two specific subprojects: Spring Data JPA and Spring Data Redis. 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 you can use the Redis key-value store as data storage and to use its other features for the purpose of enhancing the performance of your applications.

This practical guide proves that implementing JPA repositories can be fun and helps you to harness the performance of Redis in your applications.

What this book covers

Chapter 1, Getting Started, gives a brief introduction to the technologies described in this book. This chapter is divided in two parts: the first part describes the motivation behind the Java Persistence API, outlines its main concepts, and shows how you can use it for building database queries. The second part identifies the key features of the Redis key-value store.

Chapter 2, Getting Started with Spring Data JPA, helps you to start building applications by using Spring Data JPA. You will learn to set up a project that uses Spring Data JPA and configure your application by using programmatic configuration. You will also learn an easy way to create repositories for your entities and implement a simple contact manager application by using Spring Data JPA.

Chapter 3, Building Queries with Spring Data JPA, describes the techniques that you can use to build database queries. After reading this chapter, you will know how to build database queries by using query methods, JPA Criteria API, and Querydsl. You will also continue the implementation of your contact manager application by adding a search function to it.

Chapter 4, Adding Custom Functionality to JPA Repositories, teaches you how you can customize your repositories. You will learn how you can add custom functionalities either to a single repository or to all repositories. The principles discussed in this chapter are demonstrated by customizing the repositories of your contact manager application.

Chapter 5, Getting Started with Spring Data Redis, will guide you through the installation and configuration phase that is required before you can use Spring Data Redis in your applications. It describes how you can install Redis to a computer running a Unix-like operating system. Then you can set up a project that uses Spring Data Redis. In the last part of this chapter, you will learn to configure the Redis connection and compare the features of the supported connector libraries.

Chapter 6, Building Applications with Spring Data Redis, teaches you how you can use Redis in your Spring powered applications. It describes the key components of Spring Data Redis and teaches you how to use them. You will also see Spring Data Redis in action when you use Redis as data storage of your contact manager application. The last part of this chapter describes how you can use Spring Data Redis as an implementation of the Spring 3.1 cache abstraction. You will also see how to utilize Redis' publish/subscribe messaging pattern implementation in this chapter.

What you need for this book

In order to run the code examples of this book, you will need to install the following software:

  • Java 1.6

  • Maven 3.0.X

  • Redis 2.6.0-rc6

  • A web browser

If you want to experiment with the code examples, you will also need:

  • An IDE such as Eclipse, Netbeans, or IntelliJ Idea

  • The full source code package for each chapter (See the following Downloading the example code section)

Who this book is for

This book is great for developers who are working with Spring-powered applications, and are looking for an easier way to write data access code that uses relational databases. Also, if you are interested in learning how you can utilize Redis in your applications, this is the book for you. This book assumes that you have got some experience from the Spring Framework and the Java Persistence API. No previous experience from Redis is required.


In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: "We can do this by using the repositoryFactoryBeanClass property of the @EnableJpaRepositories annotation."

A block of code is set as follows:

protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) {
    return new BaseRepositoryFactory(entityManager);

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

@CachePut(value = "contacts", key="")
@Transactional(rollbackFor = NotFoundException.class)
public Contact update(ContactDTO updated) throws NotFoundException {
    //Implementation remains unchanged.

New terms and important words are shown in bold.


Warnings or important notes appear in a box like this.


Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title through the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.


Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website, or added to any list of existing errata, under the Errata section of that title.


Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.


You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.