Spring Data

Book description

Want to make it easier to implement data access with your Spring-powered applications? Then this is the book you need. A complete tutorial to Spring Data, it makes learning easier with lots of code examples and clear instructions.

  • Implement JPA repositories with lesser code
  • Includes functional sample projects that demonstrate the described concepts in action and help you start experimenting right away
  • Provides step-by-step instructions and a lot of code examples that are easy to follow and help you to get started from page one

In Detail

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

  1. Spring Data
    1. Table of Contents
    2. Spring Data
    3. Credits
    4. About the Author
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers and more
        1. Why Subscribe?
        2. Free Access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    8. 1. Getting Started
      1. Java Persistence API
        1. Key concepts
        2. Creating database queries
          1. Native SQL queries
          2. Java Persistence Query Language
          3. The Criteria API
      2. Redis
        1. Supported data types
        2. Persistence
        3. Replication
        4. Publish/subscribe messaging pattern
      3. Summary
    9. 2. Getting Started with Spring Data JPA
      1. Downloading dependencies with Maven
      2. Configuring the Spring application context
        1. Creating the properties file
        2. Creating the application context configuration class
          1. Creating the application context configuration skeleton
          2. Configuring the data source bean
          3. Configuring the entity manager factory bean
          4. Configuring the transaction manager bean
      3. Loading the application context configuration
      4. Implementing CRUD functionality for an entity
        1. Domain model
          1. Contact
            1. Creating new contact objects
            2. Updating contact information
          2. Address
            1. Creating new addresses
            2. Updating address information
        2. Creating a custom repository
          1. Creating a custom repository in the old school way
          2. Creating a custom repository with Spring Data JPA
        3. CRUD
          1. Create
          2. Read
          3. Update
          4. Delete
      5. Summary
    10. 3. Building Queries with Spring Data JPA
      1. Building queries
        1. Query methods
          1. Query generation from method name
            1. Method prefixes
            2. Property expressions
            3. Keywords
            4. Implementing the search function
            5. Pros and cons
          2. Named queries
            1. Creating a named query
            2. Creating the query method
            3. Creating the service method
            4. Pros and cons
          3. @Query annotation
            1. Creating the query method
            2. Creating the service method
            3. Pros and cons
        2. JPA Criteria API
          1. Adding the JPA Criteria API support to a repository
          2. Creating the criteria query
            1. Creating a static metamodel class
            2. Creating specifications
          3. Creating the service method
          4. Pros and cons
        3. Querydsl
          1. Configuring Querydsl-Maven integration
            1. Configuring Querydsl Maven dependencies
            2. Configuring the code generation Maven plugin
          2. Generating Querydsl query types
          3. Adding Querydsl support to a repository
          4. Creating the executed query
          5. Executing the created query
          6. Pros and cons
        4. What technique should we use?
      2. Sorting query results
        1. Sorting with method name
          1. Creating the query method
          2. Modifying the service method
        2. Sorting with query strings
          1. JPQL queries
          2. SQL queries
        3. Sorting with the Sort class
          1. JpaRepository
          2. Query generation from the method name
          3. @Query annotation
          4. JPA Criteria API
        4. Sorting with Querydsl
        5. What technique should we use?
      3. Paginating query results
        1. Changing the service layer
          1. Creating a class for pagination parameters
          2. Changing the service interface
          3. Creating PageRequest objects
        2. Implementing pagination
          1. JpaRepository
          2. Query generation from the method name
            1. Adding pagination support to the query method
            2. Modifying the service class
          3. Named queries
            1. Adding pagination support to the query method
            2. Modifying the service class
          4. @Query annotation
            1. Adding pagination support to a query method
            2. Modifying the service method
          5. JPA Criteria API
          6. Querydsl
      4. Summary
    11. 4. Adding Custom Functionality to JPA Repositories
      1. Adding custom functionality to a single repository
        1. Creating the custom interface
        2. Implementing the created interface
          1. Configuring the repository class
          2. Implementing the custom methods
        3. Creating the repository interface
        4. Creating the service implementation
        5. What did we just do?
      2. Adding custom functionality to all repositories
        1. Creating the base repository interface
        2. Implementing the base repository interface
        3. Creating the repository factory bean
          1. Creating the skeleton of the repository factory bean class
          2. Creating the repository factory inner class
          3. Creating the builder method for the repository factory
        4. Configuring Spring Data JPA
        5. Creating the repository interface
        6. Implementing the service layer
        7. What did we just do?
      3. Summary
    12. 5. Getting Started with Spring Data Redis
      1. Installing Redis
      2. Getting the required dependencies
      3. Configuring the Spring application context
      4. Configuring the Redis connection
        1. Configuring the Jedis connector
        2. Configuring the JRedis connector
        3. Configuring the RJC connector
        4. Configuring the SRP connector
      5. Summary
    13. 6. Building Applications with Spring Data Redis
      1. Designing a Redis data model
      2. Key components
        1. Atomic counters
        2. RedisTemplate
          1. Operations
          2. Serializers
      3. Implementing a CRUD application
        1. Using default serializers
          1. Configuring the application context
            1. Configuring the Redis template bean
            2. Configuring the Redis atomic long bean
          2. CRUD
            1. Create
            2. Read
            3. Update
            4. Delete
        2. Storing data in JSON
          1. Configuring the application context
            1. Configuring the value serializer bean
            2. Configuring the Redis template bean
            3. Configuring the Redis atomic long bean
          2. CRUD
            1. Create
            2. Read
            3. Update
            4. Delete
      4. The publish/subscribe messaging pattern
        1. Creating message listeners
          1. Implementing the MessageListener interface
          2. Creating a POJO message listener
        2. Configuring the application context
          1. Configuring the message listener beans
          2. Configuring the message listener adapter bean
          3. Configuring the message listener container bean
        3. Sending messages with RedisTemplate
          1. Create
          2. Update
          3. Delete
        4. Verifying the wanted behaviour
      5. Using Spring cache abstraction with Spring Data Redis
        1. Configuring the Spring cache abstraction
          1. Enabling caching annotations
          2. Configuring the host and port of the used Redis instance
          3. Configuring the Redis connection factory bean
          4. Configuring the Redis template bean
          5. Configuring the cache manager bean
        2. Identifying the cached methods
          1. Adding contact information to the cache
          2. Updating the contact information to the cache
          3. Deleting contact information from the cache
        3. Verifying that the Spring cache abstraction is working
      6. Summary
    14. Index

Product information

  • Title: Spring Data
  • Author(s): Petri Kainulainen
  • Release date: November 2012
  • Publisher(s): Packt Publishing
  • ISBN: 9781849519045