This guide walks you through the process of building an application that uses Spring Data JPA to store and retrieve data in a relational database.
What you’ll build
You’ll build an application that stores
Customer POJOs in a memory-based database.
What you’ll need
Define a simple entity
In this example, you store Customer objects, annotated as a JPA entity.
Here you have a
Customer class with three attributes, the
firstName, and the
lastName. You also have two constructors. The default constructor only exists for the sake of JPA. You won’t use it directly, so it is designated as
protected. The other constructor is the one you’ll use to create instances of
Customer to be saved to the database.
|In this guide, the typical getters and setters have been left out for brevity.|
Customer class is annotated with
@Entity, indicating that it is a JPA entity. For lack of a
@Table annotation, it is assumed that this entity will be mapped to a table named
id property is annotated with
@Id so that JPA will recognize it as the object’s ID. The
id property is also annotated with
@GeneratedValue to indicate that the ID should be generated automatically.
The other two properties,
lastName are left unannotated. It is assumed that they’ll be mapped to columns that share the same name as the properties themselves.
toString() method will print out the customer’s properties.
Create simple queries
Spring Data JPA focuses on using JPA to store data in a relational database. Its most compelling feature is the ability to create repository implementations automatically, at runtime, from a repository interface.
To see how this works, create a repository interface that works with
CustomerRepository extends the
CrudRepository interface. The type of entity and ID that it works with,
Long, are specified in the generic parameters on
CrudRepository. By extending
CustomerRepository inherits several methods for working with
Customer persistence, including methods for saving, deleting, and finding
Spring Data JPA also allows you to define other query methods by simply declaring their method signature. In the case of
CustomerRepository, this is shown with a
In a typical Java application, you’d expect to write a class that implements
CustomerRepository. But that’s what makes Spring Data JPA so powerful: You don’t have to write an implementation of the repository interface. Spring Data JPA creates an implementation on the fly when you run the application.
Let’s wire this up and see what it looks like!
Create an Application class
Here you create an Application class with all the components.
Application includes a
main() method that puts the
CustomerRepository through a few tests. First, it fetches the
CustomerRepository from the Spring application context. Then it saves a handful of
Customer objects, demonstrating the
save() method and setting up some data to work with. Next, it calls
findAll() to fetch all
Customer objects from the database. Then it calls
findOne() to fetch a single
Customer by its ID. Finally, it calls
findByLastName() to find all customers whose last name is "Bauer".
By default, Spring Boot will enable JPA repository support and look in the package (and its subpackages) where
You should see something like this:
== Customers found with findAll(): Customer[id=1, firstName='Jack', lastName='Bauer'] Customer[id=2, firstName='Chloe', lastName='O'Brian'] Customer[id=3, firstName='Kim', lastName='Bauer'] Customer[id=4, firstName='David', lastName='Palmer'] Customer[id=5, firstName='Michelle', lastName='Dessler'] == Customer found with findOne(1L): Customer[id=1, firstName='Jack', lastName='Bauer'] == Customer found with findByLastName('Bauer'): Customer[id=1, firstName='Jack', lastName='Bauer'] Customer[id=3, firstName='Kim', lastName='Bauer']
Congratulations! You’ve written a simple application that uses Spring Data JPA to save objects to a database and to fetch them — all without writing a concrete repository implementation.
|If you’re interested in exposing JPA repositories with a hypermedia-based RESTful front end with little effort, you might want to read Accessing JPA Data with REST.|