• Stars
    star
    166
  • Rank 227,748 (Top 5 %)
  • Language
    Java
  • Created about 12 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Sample code for the Spring Data JPA repositories deep dive talk

Spring Data Repositories - A Deep Dive

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 individual steps in detail

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 Customers that have an EmailAddress as well as Addresses, Products and Orders carrying LineItems placed by Customers.

Step 0 - Basic project setup

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

  • Create pom.xml
    • spring-boot-starter-parent - defaults dependency versions
    • spring-boot-starter-data-jpa - declares default dependencies (Spring Data JPA, Hibernate 4, Spring 4)
  • Sample domain classes

Step 1 - Basic JPA infrastructure setup

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

  • Add JavaConfig with @EnableAutoConfiguration to let Boot automatically configure an HSQL DataSource, a LocalContainerEntityManagerFactoryBean for Hibernate as well as a JPA transaction manager.
  • Add application.properties to tweak details of the auto-configuration
  • Add integration test to bootstrap application
  • Add integration base test to populate the database with data.sql
  • Explain ResourceDatabasePopulator

Step 2 - Quickstart

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.

  • Add CustomerRepository extends Repository<Customer, Long>
  • Add methods findOne(…), save(…), findByEmailAddress(String emailAddress)
  • @EnableJpaRepositories / <jpa:repositories />
  • Add CustomerRepositoryIntegrationTest to execute repository methods
  • Show IDE integration

Step 3 - Extended CRUD methods

Objective: Explain CRUD interface.

Customers can be deleted and obtained all at once

  • Move to CustomerRepository extends CrudRepository<Customer, Long>
  • Discuss consequences
    • all CRUD method exposed, flexibility on the client side

Step 4 - Pagination

Objective: Explain PagingAndSortingRepository interface.

Customers can be accessed page by page.

  • Switch to CustomerRepository extends PagingAndSortingRepository<Customer, Long>
  • Discuss consequences
    • even more methods exposed, but maximum flexibility for clients

Step 5 - Re-declaring existing CRUD methods

Objective: Show how users can tweak the configuration of CRUD methods or even alter return types if necessary.

CustomerRepository.findAll() should rather return a List. The transaction timeout for save(…) should be customized to 10 seconds.

  • Re-declare findAll and use List as return type
  • Re-declare save(…) and annotate with @Transactional

Step 6 - Introducing a read-only repository base interface

Objective: Explain possibilities to define custom repository base interfaces.

Orders shall be accessible in read-only mode only.

  • Introduce ReadOnlyRepository base interface
    • Include reading methods of CrudRepository and PagingAndSortingRepository
    • Fix ID type to Long
  • Introduce ProductRepository extends ReadOnlyRepository<Product>

Step 7 - Using manually defined queries

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

  • Introduce the @Query annotation
  • Mention jpa-named-queries.properties

Step 8 - Flexible predicate execution

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

  • Introduce Querydsl
    • Add Querydsl dependency and set up APT as well as IDE
    • Show generated query classes
    • Add QuerydslPredicateExecutor
    • Show usage in test case

Step 9 - Custom implementation for repositories

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.

  • Add ProductRepositoryCustom to declare custom method
  • Add custom implementation extending QueryDslRepositorySupport

More Repositories

1

spring-restbucks

Implementation of the sample from REST in Practice based on Spring projects
Java
1,231
star
2

sos

Sample projects for my talk "Refactoring to a System of Systems"
Java
242
star
3

rest-microservices

Sample for Spring Boot based REST microservices
Java
191
star
4

spring-domain-events

This has been moved to Moduliths –https://github.com/odrotbohm/moduliths.
Java
100
star
5

spring-playground

A collection of tiny helpers for building Spring applications
Java
97
star
6

arch-evident-spring

Java
85
star
7

whoops-architecture

Sample code for my talk "Whoops! Where did my architecture go?"
Java
76
star
8

lectures

Lecture scripts and slides I use during the Software Engineering course at TU Dresden
Java
68
star
9

spring-modulith-deep-dive

Sample code for the Spring Modulith Deep Dive workshop / training
Java
52
star
10

ninjector

Ninjector - the cure for Field Injection Anxiety
Java
42
star
11

spring-events-deep-dive

Sample code for my talk "A Deep Dive into Spring Application Events".
Java
40
star
12

spring-rest

Sample project for Spring 3 REST style web application
Java
30
star
13

tactical-ddd-workshop

Sample code for my Tactical DDD with Java workshop
Java
26
star
14

spring-hateoas-sample

Sample project to show Spring HATEOAS in action
Java
24
star
15

spring-five-functional-reactive

Java
20
star
16

spring-restbucks-training

Spring RESTBucks - Training
Java
17
star
17

mawspring

Modulithic Applications with Spring
Java
15
star
18

jddd-legacy

Java
10
star
19

spring-record-support

Spring extension to make JDK 14's records work OOTB with Spring libraries
Java
7
star
20

hades-showcase

Sample project to show how Hades eases JPA repository implementation.
Java
7
star
21

shop-modulith

Sample code for the presentation at betterCode() Spring
Java
7
star
22

spring-examples

Canonical samples to showcase features of dedicated Spring versions
Java
6
star
23

spring4-guided-tour

Sample code for new features in Spring 4
Java
5
star
24

maws

Sample code for Modulithic Applications with Spring
Java
5
star
25

klient

A hypermedia client API written in Kotlin
Kotlin
5
star
26

spring-jmx-namespace

JMX utility Spring namespace
Java
3
star
27

Spring-Webflow-Tag-Library

Spring Webflow tag library
Java
2
star
28

spring-tx-events

Spring transaction bound application events
Java
2
star
29

odrotbohm.github.com

Website (Production)
HTML
2
star
30

spring-boot-devtools-sample

Java
2
star
31

spring-samples

Various sample scenarios to illustrate Spring goodness
Java
2
star
32

hibernate-criteria-statistics

Sample projects on how to enable Criteria statistics in Hibernate with AspectJ until HHH-3452 gets fixed
Java
1
star
33

hibernate-bugs

Java
1
star
34

asciidoctor-pdf-example

1
star
35

spring-data-release-cli

Simple shell to execute Spring Data releases
Java
1
star
36

reactive-4te

Java
1
star
37

spring-modulith-accelerator

Java
1
star
38

pv-charger

Java
1
star
39

asciidoctor-pdf-sample

Shell
1
star
40

datanucleus-test

Java
1
star
41

scs-workshop

Self-contained systems workshop
1
star