Part 15 · Applications

Spring Data: repositories, aggregates and fetch plans

Prerequisites: 01-ecosystem

Where does repository abstraction stop?

Repository contract: query + aggregate intent; JPA model: context + relationships; JDBC model: explicit aggregates; Actual database: SQL + indexes + rows. Connections: Repository contract to JPA model (implementation); Repository contract to JDBC model (implementation); JPA model to Actual database (queries); JDBC model to Actual database (queries)
JPA and JDBC are alternatives shown together for comparison. They do not share a persistence context. [S27] [S28] [S29]

Goal & mental model verified

Spring Data builds repository abstractions around store-specific behavior. JPA has persistence-context and relationship semantics; Data JDBC uses a simpler aggregate model. Derived queries reduce boilerplate without replacing query design.

[S27] [S28]

Worked example · design exercise synthesis

A broker dashboard pages quotes by tenant and status. Use a response projection and a deliberate fetch plan instead of traversing every policy relationship during JSON serialization.

[S27] [S28] [S29]

Engineering decision synthesis

Choose JPA when its entity model helps; choose JDBC-style access when explicit aggregate operations fit. R2DBC is a non-blocking access model, not reactive JPA.

[S27] [S28] [S29]

Pitfall & diagnosis synthesis

A compact repository method can still produce expensive SQL. N+1 access and loading oversized graphs should be diagnosed through executed queries and row counts, not annotation counts.

[S27] [S28] [S29]

Improve & validate synthesis

Capture SQL and query plans for representative tenant sizes. Validate pagination and fetch plans together; compare query count and end-to-end latency after changes.

[S27] [S28] [S29]
Keep this: Repository convenience does not cancel database physics.
Check yourself: Is R2DBC just JPA returning Mono?

No. It is a different non-blocking relational access model.

Sources & further reading

  1. [S27] Spring Data query methods

    Spring project maintainers · documentation · accessed 2026-10-09 · Documentation retrieved 2026-10-09

    Supports: Derived queries and repository method contracts

    Read the linked section to validate the mechanism and its version-specific constraints.

  2. [S28] Spring Data JPA fetch plans

    Spring project maintainers · documentation · accessed 2026-10-09 · Documentation retrieved 2026-10-09

    Supports: EntityGraph; Scrolling; JPA query customization

    Read the linked section to validate the mechanism and its version-specific constraints.

  3. [S29] Why Spring Data JDBC

    Spring project maintainers · documentation · accessed 2026-10-09 · Documentation retrieved 2026-10-09

    Supports: JDBC aggregate mapping without JPA persistence context

    Read the linked section to validate the mechanism and its version-specific constraints.