Quick revision
The short answer
JPA only gives fast CRUD and a large talent pool, but advanced SQL such as LATERAL joins, window functions and CTEs is awkward in it, so teams end up with native queries anyway and with performance problems hidden behind the ORM. jOOQ or plain SQL with JdbcClient gives full database power and, with jOOQ, type-safe SQL, but needs more SQL skill, offers no automatic change tracking, and jOOQ needs a commercial licence for some databases. Over ten years, allowing both with clear guidance, JPA for aggregates and SQL for reporting and bulk work, usually matches reality best, governed through reference architectures, shared testing patterns and query observability rather than prohibition.
What to remember
In this answer, we compared standardising on JPA only, on SQL first with jOOQ or JdbcClient, or allowing both. JPA gives fast CRUD and an easy talent pool but makes advanced SQL awkward and hides performance issues, SQL first gives full database power but needs more skill and gives up automatic change tracking, with jOOQ licence costs for some databases. Over ten years, allowing both with clear guidance usually fits best, while the lasting risks are skills and engine lock-in, which we manage through reference architectures, shared testing patterns and query observability rather than prohibition.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
JPA only gives fast CRUD and a large talent pool, but advanced SQL such as LATERAL joins, window functions and CTEs is awkward in it, so teams end up with native queries anyway and with performance problems hidden behind the ORM. jOOQ or plain SQL with JdbcClient gives full database power and, with jOOQ, type-safe SQL, but needs more SQL skill, offers no automatic change tracking, and jOOQ needs a commercial licence for some databases. Over ten years, allowing both with clear guidance, JPA for aggregates and SQL for reporting and bulk work, usually matches reality best, governed through reference architectures, shared testing patterns and query observability rather than prohibition.
Background to this question
A data access standard outlives most of the services written against it. A decision made now will shape hiring, onboarding, performance incidents and migration costs for a decade, and it is hard to reverse once hundreds of repositories follow it. A senior leader is asked about this because the right answer weighs technical strengths against organisational costs over time. In this answer, we will look at what each option really gives and costs, play each forward over ten years, examine the long-term risks of skills and lock-in, and describe how to govern a mixed approach without banning anything.
1What Each Option Really Is
JPA, the Jakarta Persistence API, implemented by Hibernate in most Spring applications, maps Java entity classes to tables. We load an entity, change its fields, and at commit Hibernate works out the SQL. Spring Data JPA adds repository interfaces that generate queries from method names. Its strength is aggregate-oriented CRUD: loading an order with its lines, applying business rules and saving it, with change tracking and optimistic locking for free.
jOOQ generates Java classes from the database schema and lets us write SQL in a fluent, type-checked Java API; a renamed column breaks the build instead of production. JdbcClient, added in Spring Framework 6.1, is a thin, fluent wrapper around JDBC: we write the SQL, bind parameters and map rows to records. Both are SQL first: what we write is what the database runs.
2JPA Only: Strengths and Hidden Costs
The strengths are real. Most Java developers know JPA, simple services are quick to build, and the domain model stays in Java. For a company with many CRUD-style services, it is the cheapest way to get started.
The costs appear over time. JPQL, JPA's query language, has gained features (Hibernate 6 supports window functions and CTEs in HQL to a degree; check your version), but LATERAL joins, recursive CTEs, upserts and engine-specific features are awkward or missing, so teams write @Query(nativeQuery = true) strings anyway, with none of the type safety. Meanwhile, the ORM hides what SQL runs: N+1 queries, an eager association that loads 10,000 rows, or a flush that sends 5,000 updates one by one. These surface as production incidents, often years after the code was written.
So 'JPA only' rarely stays pure. It becomes JPA plus unmanaged native SQL, which combines the costs of both without the benefits of either.
3SQL First: Strengths and Costs
With jOOQ or JdbcClient, every database feature is available, queries are visible and reviewable, and performance is predictable because there is no hidden SQL. jOOQ adds compile-time checking against the schema, which is very valuable when the schema changes often.
The costs are on the people and the write side. Developers need real SQL skill, which is less common than JPA familiarity and needs investment. There is no automatic change tracking or dirty checking: saving an aggregate with nested lines means writing the inserts, updates and deletes ourselves, and optimistic locking is a version column we manage explicitly. And jOOQ's licensing depends on the database: its open-source edition covers open-source databases such as PostgreSQL and MySQL, while commercial databases need a paid licence (check the current terms). For a company with Oracle or SQL Server estates, that is a budget line for ten years.
4Playing Each Choice Forward Ten Years
A useful exercise is to imagine each choice at year ten. With JPA only, the company has hundreds of services where most reads are fine, a long tail of performance incidents caused by ORM behaviour, and a growing body of untyped native queries for reporting. With SQL first everywhere, the reporting and bulk paths are excellent, but simple CRUD services carry more boilerplate, and new hires take longer to become productive. With both under guidance, CRUD services use JPA, reporting and bulk paths use jOOQ or JdbcClient, and the boundary is a documented pattern rather than an accident.
| Concern | JPA only | SQL first | Both with guidance |
|---|---|---|---|
| CRUD speed | Fast | Slower, more code | Fast |
| Reports and bulk work | Native SQL strings | Excellent | Excellent |
| Hidden performance issues | Common | Rare | Contained to JPA paths |
| Hiring and onboarding | Easiest | Needs SQL skill | Two skills, clear rules |
| Licence cost | None | jOOQ for some databases | Only where jOOQ is used |
5Long-Term Risks: Skills and Lock-In
Over ten years the biggest risks are not libraries but skills and lock-in. If nobody in a team can read an execution plan, the choice of library does not matter; both JPA and SQL can be slow. So the standard includes training in SQL and plan reading, whichever library is used.
Lock-in comes through engine-specific SQL. A report that uses PostgreSQL's DISTINCT ON, LATERAL and JSONB operators will not run on another engine without rewriting. That is often an acceptable trade, since migrating databases is rare, but it should be a conscious one. The guidance should state the portability expectation, for example 'PostgreSQL-specific features are allowed; we do not promise engine portability', so teams do not pay for portability nobody needs, or assume a portability they do not have.
6Governing a Mixed Approach Without Prohibition
Prohibition fails because teams route around it, as the native-query example showed. Governance works better through three things. Reference architectures show the standard way to structure a service with a JPA write model and a SQL read model, with example code. Shared testing patterns, such as Testcontainers-based repository tests against the real engine, make SQL safe to change. And query observability, pg_stat_statements on the database side and metrics on query counts and durations from the application, makes the cost of each data access path visible, so problems are found by evidence rather than by rule.
With these in place, the rule itself can be short: use JPA where you load, change and save aggregates; use SQL where you aggregate, rank, report or process in bulk; test both against the real database; and look at the top queries every month.
- API and application servicescommands and queries separated
- Write model: JPA entities and repositoriesaggregates, change tracking, optimistic locking
- Read model: jOOQ or JdbcClientreports, lists, top-N, bulk updates
- Repository tests on Testcontainerssame engine and major version as production
- PostgreSQL with pg_stat_statementsquery observability for both paths
5 layers, from the top down. Step through them one at a time.
7Follow-up questions
How do you stop JPA and SQL in one service from stepping on each other?
Keep bulk SQL updates out of transactions that also hold managed entities, or clear the persistence context after them, because Hibernate's first-level cache does not see changes made by plain SQL. Read-model queries return records or DTOs, never managed entities, so there is no confusion about what is tracked.
Would you migrate existing JPA-only services?
Not wholesale. I would move individual queries when they cause measured problems, for example a report that loads entities to aggregate in Java, to the SQL read model, and leave the rest. The guidance applies to new code and to code being changed anyway.
Is Spring Data JDBC an option?
Yes, it sits between the two: it maps aggregates to tables without lazy loading or a persistence context, so its behaviour is simpler and more predictable than JPA's, at the cost of fewer features. It suits teams that like the repository style but have been hurt by ORM surprises.
8Conclusion
In this answer, we compared standardising on JPA only, on SQL first with jOOQ or JdbcClient, or allowing both. JPA gives fast CRUD and an easy talent pool but makes advanced SQL awkward and hides performance issues, SQL first gives full database power but needs more skill and gives up automatic change tracking, with jOOQ licence costs for some databases. Over ten years, allowing both with clear guidance usually fits best, while the lasting risks are skills and engine lock-in, which we manage through reference architectures, shared testing patterns and query observability rather than prohibition.
Open with
I'd frame it as costs over ten years rather than which library is better. JPA only is fast for CRUD and easy to hire for, but advanced SQL like LATERAL, window functions and recursive CTEs is awkward in it, so teams end up writing untyped native queries anyway, and the ORM hides performance problems like N+1 and inefficient flushes until they become incidents.
Then, if they want more
SQL first with jOOQ or JdbcClient gives full database power, visible queries and, with jOOQ, compile-time checks against the schema, but it needs stronger SQL skills, loses automatic change tracking for aggregates, and jOOQ needs a commercial licence for commercial databases.
In practice, allowing both with clear guidance matches reality: JPA for loading and saving aggregates, SQL for reporting, ranking and bulk work, with read models returning DTOs. The long-term risks are skills, if nobody can read a plan nothing helps, and lock-in through engine-specific SQL, so I'd state our portability expectation explicitly.
And I'd govern with reference architectures, Testcontainers testing patterns, and query observability through pg_stat_statements and application metrics, not with bans, because bans just push the SQL into places where nobody reviews it.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Hibernate 6, Spring Framework 6
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, The SQL Language
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Your company is deciding whether to standardise data access in Java services on JPA...": https://scrivio-interview-library.netlify.app/databases/advanced-sql/fe38268f95/