This repository contains the sample code for the "Spring Data Repositories - A Deep Dive" presentation that demonstrates the feature of the Spring Data repositories abstraction in detail.
The repository is not a continuous one in the classical sense. Every commit represents a step in the presentation and implements a new requirement as the code continues to grow.
Attention: Expect the repository to be rebased at any time as changes to the codebases have to be applied to the commit the relevant code was introduced.
The domain model is a tiny CRM system in the first place but will be extended into an e-commerce system later on. Core abstractions are Customer
s that have an EmailAddress
as well as Address
es, Product
s and Order
s carrying LineItem
s placed by Customers
.
Objective: Learn how to quickly create a new Maven based project using Spring Boot for easy dependency management and Spring auto configuration.
How to get a Spring Data JPA based project up and running quickly
pom.xml
spring-boot-starter-parent
- defaults dependency versionsspring-boot-starter-data-jpa
- declares default dependencies (Spring Data JPA, Hibernate 4, Spring 4)Objective: Learn in how far Boot simplifies the setup and the configuration of an ApplicationContext
. Learn about JPA extensions introduced with Spring 3.1, and possibilities to populate a DataSource
.
Persistence technology of choice is JPA. The application uses JavaConfig and sample data contained in data.sql
@EnableAutoConfiguration
to let Boot automatically configure an HSQL DataSource
, a LocalContainerEntityManagerFactoryBean
for Hibernate as well as a JPA transaction manager.application.properties
to tweak details of the auto-configurationdata.sql
Objective: Show basics of interface based programming model, query derivation and infrastructure setup.
The implementation of the persistence layer will be based on the Spring Data repositories abstraction. Customers can be saved, looked up by their id, email address.
CustomerRepository extends Repository<Customer, Long>
findOne(…)
, save(…)
, findByEmailAddress(String emailAddress)
@EnableJpaRepositories
/ <jpa:repositories />
CustomerRepositoryIntegrationTest
to execute repository methodsObjective: Explain CRUD interface.
Customers can be deleted and obtained all at once
CustomerRepository extends CrudRepository<Customer, Long>
Objective: Explain PagingAndSortingRepository interface.
Customers can be accessed page by page.
CustomerRepository extends PagingAndSortingRepository<Customer, Long>
Objective: Show how users can tweak the configuration of CRUD methods or even alter return types if necessary.
CustomerRepository.findAll()
should rather return aList
. The transaction timeout forsave(…)
should be customized to 10 seconds.
findAll
and use List
as return typesave(…)
and annotate with @Transactional
Objective: Explain possibilities to define custom repository base interfaces.
Orders shall be accessible in read-only mode only.
ReadOnlyRepository
base interface
CrudRepository
and PagingAndSortingRepository
Long
ProductRepository extends ReadOnlyRepository<Product>
Objective: Learn how to manually define a query using the @Query
annotation or named queries
As a user, I want to look up products by their custom attributes
@Query
annotationjpa-named-queries.properties
Objective: Learn how to define atomic business predicates and execute them in flexible ways.
As a user, I want to search for customers by first name, last name, email address and any combination of them
QuerydslPredicateExecutor
Objective: Lear how to extend a repository with manually implemented code.
As an admin user, I'd like to delete all products beyond a given price.
ProductRepositoryCustom
to declare custom methodQueryDslRepositorySupport