Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
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.

Arrow left icon
Profile Icon Petri Kainulainen
Arrow right icon
$19.99 per month
Full star icon Full star icon Full star icon Full star icon Half star icon 4.5 (8 Ratings)
Paperback Nov 2012 160 pages 1st Edition
eBook
$13.98 $19.99
Paperback
$26.99
Subscription
Free Trial
Renews at $19.99p/m
Arrow left icon
Profile Icon Petri Kainulainen
Arrow right icon
$19.99 per month
Full star icon Full star icon Full star icon Full star icon Half star icon 4.5 (8 Ratings)
Paperback Nov 2012 160 pages 1st Edition
eBook
$13.98 $19.99
Paperback
$26.99
Subscription
Free Trial
Renews at $19.99p/m
eBook
$13.98 $19.99
Paperback
$26.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
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 : 9781849519045
Vendor :
Pivotal
Category :
Languages :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Nov 05, 2012
Length: 160 pages
Edition : 1st
Language : English
ISBN-13 : 9781849519045
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 MVC Beginner's Guide
$48.99
Spring Data
$26.99
Total $ 75.98 Stars icon

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

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.