20,39 €
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.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 180
Veröffentlichungsjahr: 2012
Copyright © 2012 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: November 2012
Production Reference: 2291012
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-849519-04-5
www.packtpub.com
Cover Image by Abhishek Pandey (<[email protected]>)
Author
Petri Kainulainen
Reviewers
Antti Koivisto
Jari Timonen
Timo Westkämper
Acquisition Editor
Usha Iyer
Commissioning Editor
Priyanka Shah
Technical Editor
Jalasha D'costa
Project Coordinator
Shraddha Bagadia
Proofreader
Maria Gould
Indexer
Hemangini Bari
Graphics
Aditi Gajjar
Production Coordinator
Arvindkumar Gupta
Cover Work
Arvindkumar Gupta
Petri Kainulainen is a software developer living in Tampere, Finland. He is specialized in application development with the Java programming language and the Spring framework. Petri has over 10 years of experience in software development, and during his career he has participated in the development projects of Finland's leading online market places as a software architect. He is currently working at Vincit Oy as a passionate software developer.
I would like to thank my employer Vincit Oy for providing me with an endless supply of printer paper and an inspiring work environment that encouraged me to accept this challenge.
Writing this book would not have been possible without the help of my reviewers. I would like to thank Antti Koivisto, Jari Timonen, and Timo Westkämper for their comments and improvement ideas.
Finally, I would like to thank my family and friends for their support and encouragement. I am especially grateful to my mom and dad who bought me my first computer as a Christmas present when I was eight years old. Without this present, I would have probably chosen another profession and I would have never written this book.
Antti Koivisto is a Java EE architect, data transfer system specialist, data modeler, continuous delivery enthusiast, and a TDD practitioner. Antti has worked on all layers of n-tier web applications, all the way from the Linux server administration to jQuery and Search Engine Optimization (SEO). His weapons of choice come from SpringSource and his philosophy of "convention over configuration" comes from Ruby on Rails. Currently Antti works at Alma Mediapartners on the largest classified ad sites of Finland: Etuovi.com and Autotalli.com. Antti goes by @koantweet on Twitter.
Jari Timonen is an experienced software enthusiast with over 10 years of experience in the software industry. His experience includes successful team leadership combined with understanding complex business domains and delivering them into practice. Jari has been building enterprise architectures, designing software, and programming. While he started his career in the finance industry, he currently works as a Solution Architect in a telecommunications company. He practices pair programming and is keen on studying new technologies. When he is not building software, he is spending time with his family, fishing, or flying his radio controlled model helicopter.
Jari currently owns following certifications: Sun Certified Programmer for the Java 2 Platform, Standard Edition 5 (SCJP), Sun Certified Developer for the Java 2 Platform (SCJD), and Oracle Certified Master, Java EE 5 Enterprise Architect (OCMJEA).
Timo Westkämper is a Software Architect working at Java Enterprise consulting in the capital region of Finland. He has been working with Java technologies since 2004 and is especially interested in DSLs, query languages, and new languages for the JVM platform.
He is also the co-founder of Mysema and the maintainer of the popular querying library QueryDSL, which is tightly integrated into the Spring Data framework.
You might want to visit www.PacktPub.com for support files and downloads related to your book.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at <[email protected]> for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
http://PacktLib.PacktPub.com
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can access, read and search across Packt's entire library of books.
If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access.
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: http://www.springsource.org/spring-data/.
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.
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.
In order to run the code examples of this book, you will need to install the following software:
If you want to experiment with the code examples, you will also need:
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.
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 <[email protected]>, 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 www.packtpub.com/authors.
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.
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support 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 http://www.packtpub.com/support, 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 <[email protected]> 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 <[email protected]> if you are having a problem with any aspect of the book, and we will do our best to address it.
In this book, we will concentrate on two specific subprojects that offer support for Java Persistence API 2.0 and the Redis key-value store. But before we get to the point, we need to get a brief introduction to both the technologies. We need to do this for two reasons:
First, if we want to truly understand the benefits of Spring Data JPA, we need to have an idea on how database queries are created when the standard API is used. As soon as we compare these code samples to a query creation code that uses Spring Data JPA, its benefits are revealed to us.
Second, the basic knowledge about the Redis key-value store will help us to understand the second part of this book which describes how we can use it in our applications. After all, we should be familiar with any technology that we use in our applications. Right?
In this chapter, we will cover the following topics:
Before the Java Persistence API (JPA) was introduced, we had the following three alternative technologies which we could use to implement our persistence layer:
This gave us some freedom when selecting the best tool for the job but as always, none of these options were problem free.
The problem with EJB 2.x was that it was too heavyweight and complicated. Its configuration relied on complicated XML documents and its programming model required a lot of boilerplate code. Also, EJB required that the application be deployed to a Java EE application server.
Programming against the JDBC API was rather simple and we could deploy our application in any servlet container. However, we had to write a lot of boilerplate code that was needed when we were transforming the information of our domain model to queries or building domain model objects from query results.
Third party ORM frameworks were often a good choice because they freed us from writing the unnecessary code that was used to build queries or to construct domain objects from query results. This freedom came with a price tag: objects and relational data are not compatible creatures, and even though ORM frameworks can solve most of the problems caused by the object-relational mismatch, the problems that they cannot solve efficiently are the ones that cause us the most pain.
