Video in progress

Staff 🌱 Spring

You need to choose the data layer for a new service: Spring Data JPA, jOOQ or JdbcClient. What criteria do you decide by?

JPA is productive with a rich domain model and lots of CRUD; jOOQ is for complex, type-checked SQL and reports; JdbcClient is simple, transparent and has the least abstraction. Often two of them work together in one service.

📖 Answer

I ask three questions: what shape the data has, how complex the queries are, and what the team knows well.

Criterion Spring Data JPA jOOQ JdbcClient
CRUD speed (of development) Very high Medium Low (SQL by hand)
Complex SQL (window, CTE) Awkward, you drop to native Strong, checked at compile time Full freedom, as text
Visibility of the executed SQL Hidden (flush, lazy load, N+1) Explicit Explicit
Learning curve and pitfalls Many (proxies, persistence context) Medium Few
License Free Paid for commercial databases Free

I pick JPA if: the aggregates are clear (an order + its lines), there is a lot of write and read by id, and you need optimistic locking and auditing. The team knows Hibernate deeply.

I pick jOOQ if: the service is mostly reports and search, the queries are dynamic and complex, and we work schema-first (generating code from the Flyway migrations). A wrong SQL gets caught at compile time.

JdbcClient fits small services, batch writes, a high-load write path, or a narrow spot inside a JPA service:

List<TopProduct> top = jdbc.sql("""
        SELECT product_id, sum(qty) AS total
        FROM order_line WHERE created_at > :from
        GROUP BY product_id ORDER BY total DESC LIMIT 10""")
    .param("from", from)
    .query(TopProduct.class)
    .list();

A mixed approach is normal: the write side with JPA (aggregates, invariants), reads and reports with jOOQ/JdbcClient. Just one rule: if both are in the same transaction, make sure Hibernate has flushed before the JdbcClient query, otherwise JDBC won't see changes that haven't been sent yet.

At the organizational level, I don't say "let each team pick what it wants". I offer 1–2 recommended paths and a ready template for them, otherwise you end up supporting 5 different approaches across 30 services.