Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Spring Data
Spring Data

Spring Data: 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.

eBook
$9.99 $19.99
Paperback
$26.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Spring Data

Chapter 2. Getting Started with Spring Data JPA

This chapter gives us the basic knowledge that we need to set up a web application project and manage our entities by using Spring Data JPA. In the course of this chapter, we will learn:

  • How to use Maven for downloading the required dependencies

  • How to use programmatic configuration for configuring the Spring application context

  • How to configure our web application to load the Spring application context by using programmatic configuration (without using web.xml)

  • How to implement CRUD (Create, Read, Update, and Delete) functions for an entity class with Spring Data JPA

Downloading dependencies with Maven


This book covers the 1.2.0.RELEASE version of Spring Data JPA, which is the newest available version during the authoring of this book. The other components required by Spring Data JPA are described in the following table:

Component

Description

Version

Data source

BoneCP is a fast connection pool library that is used as a data source for our application.

0.7.1.RELEASE

JPA provider

A JPA provider is a library that implements the Java Persistence API. We will use Hibernate as a JPA provider.

4.1.4.Final

Spring Framework

Spring Framework is used to develop modern enterprise applications with Java.

3.1.2.RELEASE

Database

H2 is an embedded in-memory database that supports standard SQL and the JDBC API.

1.3.166

We will use the newest available version of our application's other dependencies.

We can download the required dependencies with Maven by declaring them in the POM file. In order to do this, we have to add the following dependency declarations...

Configuring the Spring application context


Traditionally, we would use declarative configuration with XML configuration files, but after Spring Framework 3.0 was released, it has been possible to configure the Spring application context by using programmatic configuration. This is our weapon of choice when we are configuring the application context of our application.

We can configure the Spring application context by following these steps:

  1. Create a properties file for the values of the configuration parameters.

  2. Create the application context configuration class.

Creating the properties file

The actual values of the configuration parameters are stored in a properties file called application.properties. This file contains database connection details, Hibernate configuration, and the base package of our entities. The content of this file is as follows:

#Database Configuration
db.driver=org.h2.Driver
db.url=jdbc:h2:mem:datajpa
db.username=sa
db.password=

#Hibernate Configuration
hibernate.dialect...

Loading the application context configuration


The old way to load the application context configuration of our application is to use the web application deployment descriptor file, which is more commonly known as web.xml. However, because we are using the Spring Framework 3.1 in a Servlet 3.0 environment, we can create a web application configuration class by implementing the WebApplicationInitializer interface. This ensures that Spring Framework automatically detects our configuration class when a servlet container is started.

We will use our web application configuration class to:

  1. Load our application context configuration class.

  2. Configure the dispatcher servlet.

  3. Create the context loader listener and add it to our servlet context.

The source code of our configuration class is given as follows:

public class DataJPAExampleInitializer implements WebApplicationInitializer {
        
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        //Loading...

Implementing CRUD functionality for an entity


We have now configured the Spring application context and configured our web application to load it during startup. We will now implement CRUD functions for a simple entity. Our example application is used to view and manage contact information, and we can implement it by following these steps:

  1. Create a domain model.

  2. Create a repository for an entity.

  3. Implement CRUD functions.

Note

This chapter describes only such parts of our application that are required to understand how Spring Data JPA works.

Domain model

The domain model of our application consists of two classes: Contact and Address. This subsection will address the following matters:

  • The information content of each class

  • How we can create new objects by using the builder pattern (see also: Effective Java (Second Edition), Joshua Bloch, Addison-Wesley)

  • How we can update the information of an object

Contact

The Contact class is the only entity of our domain model and it contains the information of a...

Summary


In this chapter, we have learned that:

  • Maven provides an easy way to set up a Spring Data JPA project

  • We can configure the application context of our application by using programmatic configuration if we use Spring Framework 3.0 or newer versions

  • If we use Spring Framework 3.1 in a Servlet 3.0 environment, we can configure our web application without web.xml

  • Spring Data JPA simplifies the creation of custom repositories because it can automatically create concrete implementations of our repository interfaces

Building a CRUD application is a good start but it does not help us create real-life applications. In the next chapter, we will address this issue and describe how we can create database queries with Spring Data JPA.

Left arrow icon Right arrow icon

Key benefits

  • 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

Description

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.

Who is this book for?

This book is suited 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 with the Spring Framework and the Java Persistence API. No previous experience with Redis is required.

What you will learn

  • Configure your application by using Java configuration
  • Manage the information of your entities through repository interfaces
  • Learn efficient and easy ways to create database queries
  • Use an easy way to sort and paginate query results
  • Customize your JPA repositories with custom functions
  • Install Redis to a computer running Unix-like operating system
  • Use the Redis key-value store as the data storage for your application
  • Utilize the Redis publish/subscribe messaging pattern implementation

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Nov 05, 2012
Length: 160 pages
Edition : 1st
Language : English
ISBN-13 : 9781849519052
Vendor :
Pivotal
Category :
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Nov 05, 2012
Length: 160 pages
Edition : 1st
Language : English
ISBN-13 : 9781849519052
Vendor :
Pivotal
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 75.98
Spring Data
$26.99
Spring MVC Beginner's Guide
$48.99
Total $ 75.98 Stars icon
Banner background image

Table of Contents

6 Chapters
Getting Started Chevron down icon Chevron up icon
Getting Started with Spring Data JPA Chevron down icon Chevron up icon
Building Queries with Spring Data JPA Chevron down icon Chevron up icon
Adding Custom Functionality to JPA Repositories Chevron down icon Chevron up icon
Getting Started with Spring Data Redis Chevron down icon Chevron up icon
Building Applications with Spring Data Redis Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.5
(8 Ratings)
5 star 62.5%
4 star 25%
3 star 12.5%
2 star 0%
1 star 0%
Filter icon Filter
Top Reviews

Filter reviews by




MrPekka Dec 04, 2012
Full star icon Full star icon Full star icon Full star icon Full star icon 5
The book gives a good and quick guide to start working with Spring Framework's Data API. The structure and examples are very practical and approachable and provide insight into Spring Data which is difficult to find from the Spring Frameworks online API-documentation. In all I found this book to be good and easy to read introductory book on the subject.
Amazon Verified review Amazon
thalassa Mar 15, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
It will be a real pleasure to order again from this seller, it weas everything as I wish, as I order. Thanks a lot and wish you the best.
Amazon Verified review Amazon
Leandro Siciliano Jun 13, 2013
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Excellent book ! I could learn a lot with this book and autor website. Perfect !!! Before I read, I had a lot of doubts. Recommend
Amazon Verified review Amazon
H. E. Jan 02, 2013
Full star icon Full star icon Full star icon Full star icon Full star icon 5
It's really simple book with simple examples. It gives you basic knowledgement of Spring Data and easy way to start learning this technology.
Amazon Verified review Amazon
Mr. Magnus H. Smith Aug 20, 2013
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I found this was a good book for getting upto speed quickly with spring data. The examples on github are pretty good and the author takes a lot of care to write good tests. The authors website is pretty useful too
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.