Spring Data JDBC

Spring Data

Let’s take a look

a.k.a Demo

You need

  • spring-boot-starter-data-jdbc

  • Aggregate Roots need an @Id

  • No getters/setter required

  • You can "wither" instead

Why Spring Data Jdbc?

We have already JPA

JPAs complexity

Lazy Loading (Exception)

Dirty Checking

Session / 1st Level Cache

Proxies for Entities

Map almost anything to anything

Spring Data JDBCs Design Choices

No Lazy Loading

No Caching

No Proxies

No deferred flushing

Very simple & limited & opinionated ORM

Simplicity is King

Complexity is tempting

Let’s see that in action

a.k.a more demo code

  • Clone: set ID to null and save again.

  • Loading the same entity creates multiple instances.

Death is for free

and it costs you your life.

Non trivial Aggregates

Design Challenge

CRUD Operations for entities with references

more Demo

One to N is fine

But what about Many-to-X?

⇒ No Many-to-X relations!

Aggregate

Aggregate is a pattern in Domain-Driven Design. A DDD aggregate is a cluster of domain objects that can be treated as a single unit.

https://martinfowler.com/bliki/DDD_Aggregate.html
— Martin Fowler

Repositories work on Aggregates

Aggregates are the basic element of transfer of data storage - you request to load or save whole aggregates.

https://martinfowler.com/bliki/DDD_Aggregate.html
— Martin Fowler

Aggregate Root

An aggregate will have one of its component objects be the aggregate root. Any references from outside the aggregate should only go to the aggregate root. The root can thus ensure the integrity of the aggregate as a whole.

https://martinfowler.com/bliki/DDD_Aggregate.html
— Martin Fowler

Many to X relations

If multiple Aggregate roots reference X,

X must be another Aggregate Root.

⇒ Not part of this Aggregate.

What is part of the Aggregate?

Everything referenced.

But Many-to-X is real!

  • Just reference an id.

Let’s look at code

Effects

  • Makes Aggregates obvious in code

  • Easier testing (no huge chains of dependencies)

  • Allows to pick a persistence strategy per Aggregate

Events

Available Events

  • BeforeSaveEvent

  • AfterSaveEvent

  • BeforeDeleteEvent

  • AfterDeleteEvent

  • AfterLoadEvent - when an Aggregate got instantiated from the database

AggregateChange

  • A model of the changes to be applied to the database

  • contains a mutable list of DbAction s.

DbAction

Represents a single SQL statement to be executed against the database.

Do whatever you want

  • Access the repository

  • change the entities

  • Change the AggregateChange!

Auditing

Enable Auditing Support

@EnableJdbcAuditing

Add fields

@CreatedBy
String createdBy;
@CreatedDate
Instant createdAt;
@LastModifiedBy
String updatedBy;
@LastModifiedDate
Instant updatedAt;

Register AuditorAware

@Bean
AuditorAware<String> auditorAware() {
  return new ModifiableAuditorAware();
}

AuditorAware example

public class ModifiableAuditorAware
    implements AuditorAware<String> {

  String value;

  @Override
  public Optional<String> getCurrentAuditor() {
    return Optional.ofNullable(value);
  }
}

MyBatis

@Query

@Query

@Query("select count(*) from address")
int countAddresses();

@Modifying

RowMapper

(Maybe) upcoming

Not promising anything.

Better CRUD

upserts?

Derived Queries

Sorting and Paging

Named Queries with MyBatis

Named Queries via Properties

Spring Boot Support

Demo used Spring Boot 2.1.0.M4

Is there reactive support?

The challenge

JDBC is blocking

Could be wrapped in a ThreadPool which would be a compromise.

JPA won’t happen

The whole approach is opposite to reactive.

ADBA

Asynchronous DataBase Api being developed by Oracle

AoJ

ADBA over JDBC

An rudimentary implementation of ADBA based on JDBC.

R2DBC

Reactive Relational Database Connectivity

R2DBC Contributers

  • Pivotal (Spring, Spring Data, Reactor)

  • jOOQ (Lukas Eder)

  • RxJava (D├ívid Karnok)

R2DBC Experimental Implementations

R2DBC @ Spring Data

Database Client inspired by Webclient.

Reactive Relational Repositories.

Where is this heading?

ADBA might actually become [R2DBC], and to some extent that is the end goal of a project like this.

Spring One Platform R2DBC talk
— Ben Hale

But make no mistake, I am not the sort of person who is going to tolerate the ADBA spec being bad.

Spring One Platform R2DBC talk
— Ben Hale

3 Implementations of R2DBC started with in a week after the announcement.

References

Jens Schauder

Dad, Board&Roleplaying Gamer, Runner, Bouldering, Freeletics Masochist

+

JUG Organizer

Questions?