Spring Data
()
About this ebook
Related to Spring Data
Related ebooks
Building a RESTful Web Service with Spring Rating: 5 out of 5 stars5/5Spring Essentials Rating: 0 out of 5 stars0 ratingsMastering Spring 5.0 Rating: 5 out of 5 stars5/5Distributed Computing in Java 9 Rating: 0 out of 5 stars0 ratingsSpring Boot in Practice Rating: 0 out of 5 stars0 ratingsASP.NET Core 3 and React: Hands-On full stack web development using ASP.NET Core, React, and TypeScript 3 Rating: 0 out of 5 stars0 ratingsBuilding Scalable Apps with Redis and Node.js Rating: 0 out of 5 stars0 ratingsPerformance Testing with JMeter 2.9 Rating: 0 out of 5 stars0 ratingsTesting Java Microservices: Using Arquillian, Hoverfly, AssertJ, JUnit, Selenium, and Mockito Rating: 0 out of 5 stars0 ratingsSpring Boot Cookbook Rating: 0 out of 5 stars0 ratingsSpring Boot 2: How To Get Started and Build a Microservice - Third Edition Rating: 5 out of 5 stars5/5Spring MVC Cookbook Rating: 0 out of 5 stars0 ratingsSpring Security 3.x Cookbook Rating: 0 out of 5 stars0 ratingsSpring Security in Action Rating: 0 out of 5 stars0 ratingsHow To Build Microservices: Top 10 Hacks To Modeling, Integrating & Deploying Microservices Rating: 0 out of 5 stars0 ratingsJUnit in Action Rating: 0 out of 5 stars0 ratingsSpring Microservices Rating: 0 out of 5 stars0 ratingsSpring MVC: Designing Real-World Web Applications Rating: 0 out of 5 stars0 ratingsMastering Unit Testing Using Mockito and JUnit Rating: 0 out of 5 stars0 ratingsSpring Cookbook Rating: 0 out of 5 stars0 ratingsSpring in Action Rating: 4 out of 5 stars4/5Test-Driven Java Development Rating: 4 out of 5 stars4/5Angular Development with TypeScript Rating: 0 out of 5 stars0 ratingsSpring in Action, Sixth Edition Rating: 5 out of 5 stars5/5Spring Boot in Action Rating: 0 out of 5 stars0 ratingsMockito Essentials Rating: 3 out of 5 stars3/5Spring Microservices in Action Rating: 0 out of 5 stars0 ratings
Information Technology For You
Creating Online Courses with ChatGPT | A Step-by-Step Guide with Prompt Templates Rating: 4 out of 5 stars4/5Health Informatics: Practical Guide Rating: 0 out of 5 stars0 ratingsData Analytics for Beginners: Introduction to Data Analytics Rating: 4 out of 5 stars4/5How to Write Effective Emails at Work Rating: 4 out of 5 stars4/5CompTIA A+ CertMike: Prepare. Practice. Pass the Test! Get Certified!: Core 1 Exam 220-1101 Rating: 0 out of 5 stars0 ratingsHacking Essentials - The Beginner's Guide To Ethical Hacking And Penetration Testing Rating: 3 out of 5 stars3/5Cybersecurity for Beginners : Learn the Fundamentals of Cybersecurity in an Easy, Step-by-Step Guide: 1 Rating: 0 out of 5 stars0 ratingsHow To Use Chatgpt: Using Chatgpt To Make Money Online Has Never Been This Simple Rating: 0 out of 5 stars0 ratingsChatGPT: The Future of Intelligent Conversation Rating: 4 out of 5 stars4/5Computer Science: A Concise Introduction Rating: 4 out of 5 stars4/5Inkscape Beginner’s Guide Rating: 5 out of 5 stars5/5An Ultimate Guide to Kali Linux for Beginners Rating: 3 out of 5 stars3/5Unity Game Development Essentials Rating: 5 out of 5 stars5/5Investigating Child Exploitation and Pornography: The Internet, Law and Forensic Science Rating: 5 out of 5 stars5/5Learning Website Development with Django Rating: 0 out of 5 stars0 ratingsPersonal Knowledge Graphs: Connected thinking to boost productivity, creativity and discovery Rating: 0 out of 5 stars0 ratingsSupercommunicator: Explaining the Complicated So Anyone Can Understand Rating: 3 out of 5 stars3/5Windows Registry Forensics: Advanced Digital Forensic Analysis of the Windows Registry Rating: 4 out of 5 stars4/5Linux Command Line and Shell Scripting Bible Rating: 3 out of 5 stars3/5Data Governance For Dummies Rating: 0 out of 5 stars0 ratingsThe Basics of Hacking and Penetration Testing: Ethical Hacking and Penetration Testing Made Easy Rating: 4 out of 5 stars4/5An Executive Guide to Identity Access Management - 2nd Edition Rating: 4 out of 5 stars4/5Computer Organization and Design: The Hardware / Software Interface Rating: 4 out of 5 stars4/5Panda3d 1.7 Game Developer's Cookbook Rating: 0 out of 5 stars0 ratingsSummary of Super-Intelligence From Nick Bostrom Rating: 5 out of 5 stars5/5Practical Ethical Hacking from Scratch Rating: 5 out of 5 stars5/5CompTIA Network+ CertMike: Prepare. Practice. Pass the Test! Get Certified!: Exam N10-008 Rating: 0 out of 5 stars0 ratings
Reviews for Spring Data
0 ratings0 reviews
Book preview
Spring Data - Petri Kainulainen
Table of Contents
Spring Data
Credits
About the Author
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers and more
Why Subscribe?
Free Access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Getting Started
Java Persistence API
Key concepts
Creating database queries
Native SQL queries
Java Persistence Query Language
The Criteria API
Redis
Supported data types
Persistence
Replication
Publish/subscribe messaging pattern
Summary
2. Getting Started with Spring Data JPA
Downloading dependencies with Maven
Configuring the Spring application context
Creating the properties file
Creating the application context configuration class
Creating the application context configuration skeleton
Configuring the data source bean
Configuring the entity manager factory bean
Configuring the transaction manager bean
Loading the application context configuration
Implementing CRUD functionality for an entity
Domain model
Contact
Creating new contact objects
Updating contact information
Address
Creating new addresses
Updating address information
Creating a custom repository
Creating a custom repository in the old school way
Creating a custom repository with Spring Data JPA
CRUD
Create
Read
Update
Delete
Summary
3. Building Queries with Spring Data JPA
Building queries
Query methods
Query generation from method name
Method prefixes
Property expressions
Keywords
Implementing the search function
Pros and cons
Named queries
Creating a named query
Creating the query method
Creating the service method
Pros and cons
@Query annotation
Creating the query method
Creating the service method
Pros and cons
JPA Criteria API
Adding the JPA Criteria API support to a repository
Creating the criteria query
Creating a static metamodel class
Creating specifications
Creating the service method
Pros and cons
Querydsl
Configuring Querydsl-Maven integration
Configuring Querydsl Maven dependencies
Configuring the code generation Maven plugin
Generating Querydsl query types
Adding Querydsl support to a repository
Creating the executed query
Executing the created query
Pros and cons
What technique should we use?
Sorting query results
Sorting with method name
Creating the query method
Modifying the service method
Sorting with query strings
JPQL queries
SQL queries
Sorting with the Sort class
JpaRepository
Query generation from the method name
@Query annotation
JPA Criteria API
Sorting with Querydsl
What technique should we use?
Paginating query results
Changing the service layer
Creating a class for pagination parameters
Changing the service interface
Creating PageRequest objects
Implementing pagination
JpaRepository
Query generation from the method name
Adding pagination support to the query method
Modifying the service class
Named queries
Adding pagination support to the query method
Modifying the service class
@Query annotation
Adding pagination support to a query method
Modifying the service method
JPA Criteria API
Querydsl
Summary
4. Adding Custom Functionality to JPA Repositories
Adding custom functionality to a single repository
Creating the custom interface
Implementing the created interface
Configuring the repository class
Implementing the custom methods
Creating the repository interface
Creating the service implementation
What did we just do?
Adding custom functionality to all repositories
Creating the base repository interface
Implementing the base repository interface
Creating the repository factory bean
Creating the skeleton of the repository factory bean class
Creating the repository factory inner class
Creating the builder method for the repository factory
Configuring Spring Data JPA
Creating the repository interface
Implementing the service layer
What did we just do?
Summary
5. Getting Started with Spring Data Redis
Installing Redis
Getting the required dependencies
Configuring the Spring application context
Configuring the Redis connection
Configuring the Jedis connector
Configuring the JRedis connector
Configuring the RJC connector
Configuring the SRP connector
Summary
6. Building Applications with Spring Data Redis
Designing a Redis data model
Key components
Atomic counters
RedisTemplate
Operations
Serializers
Implementing a CRUD application
Using default serializers
Configuring the application context
Configuring the Redis template bean
Configuring the Redis atomic long bean
CRUD
Create
Read
Update
Delete
Storing data in JSON
Configuring the application context
Configuring the value serializer bean
Configuring the Redis template bean
Configuring the Redis atomic long bean
CRUD
Create
Read
Update
Delete
The publish/subscribe messaging pattern
Creating message listeners
Implementing the MessageListener interface
Creating a POJO message listener
Configuring the application context
Configuring the message listener beans
Configuring the message listener adapter bean
Configuring the message listener container bean
Sending messages with RedisTemplate
Create
Update
Delete
Verifying the wanted behaviour
Using Spring cache abstraction with Spring Data Redis
Configuring the Spring cache abstraction
Enabling caching annotations
Configuring the host and port of the used Redis instance
Configuring the Redis connection factory bean
Configuring the Redis template bean
Configuring the cache manager bean
Identifying the cached methods
Adding contact information to the cache
Updating the contact information to the cache
Deleting contact information from the cache
Verifying that the Spring cache abstraction is working
Summary
Index
Spring Data
Spring Data
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 (<abhishek.pandey1210@gmail.com>)
Credits
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
About the Author
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.
About the Reviewers
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.
www.PacktPub.com
Support files, eBooks, discount offers and more
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
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.
Why Subscribe?
Fully searchable across every book published by Packt
Copy and paste, print and bookmark content
On demand and accessible via web browser
Free Access for Packt account holders
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.
Preface
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