Scrivio Interview library
LeadershipAdvanced6 min read · updated 12 November 2026

A service has grown to 200 native queries in JPA repositories, many with copy-pasted fragments. Should the team move to a SQL-first tool such as jOOQ, back to JPQL, or keep them? How do you decide?

Two hundred native queries tell us the service's reads are really SQL-shaped, so forcing them back into JPQL rarely helps. A SQL-first tool such as jOOQ gives type-safe SQL generated from the schema and catches schema drift at compile time, and a common end state is JPA for aggregate writes and jOOQ for reads, if the team can maintain both. We decide on maintainability, test coverage and team skill, migrate gradually, and write down a rule for when native SQL is acceptable.

Why this question is asked

Every long-lived JPA service drifts this way: a native query for a report, another for a dashboard, and a few years later there are hundreds of SQL strings with the same filter fragment pasted into forty of them. Interviewers ask this as a leadership question because there is no single right tool. They want to hear how we read the situation, compare options with their real costs, and lead a team through a change without a big-bang rewrite. In this answer, we will look at what the 200 queries are telling us, compare the three options, walk through a decision with numbers, and finish with the migration and the rule that keeps the problem from coming back.

1What 200 Native Queries Are Telling Us

Before choosing a tool, we read the evidence. A team does not write 200 native queries by accident. Usually the service has grown into reporting, search screens and aggregations, work where the natural unit is a result set rather than an entity. JPA is designed for loading an aggregate, a cluster of entities we change together such as an order with its lines, changing it and saving it. Its query language is a means to that end.

So the first step is to classify the queries. In a typical case we might find 140 read queries that return DTOs for screens and reports, 40 that use PostgreSQL features such as jsonb operators or window functions, and 20 that are plain filters someone wrote natively out of habit. The copy-pasted fragments are a separate problem: they mean there is no way to compose SQL in this codebase, so people duplicate it.

2Comparing the Three Options

Moving back to JPQL fits the 20 habit queries and perhaps some of the others now that Hibernate 6 supports window functions and common table expressions in HQL. It does not fit the reports well, and JPQL strings have no composition story either, so the copy-paste would continue in a different language.

jOOQ is a library that generates Java classes from the database schema, one per table with typed fields per column, and lets us build SQL with a fluent Java API. A renamed column becomes a compile error instead of a production error, fragments become ordinary Java methods we can reuse, and the SQL we write is still SQL, including vendor features. Its costs are a code generation step in the build, a second data access style next to JPA, and licensing: the open source edition covers open source databases such as PostgreSQL and MySQL, while commercial databases such as Oracle or SQL Server need a paid edition.

Keeping the native queries is also a real option. If they are stable, well tested and owned by people who know SQL, the cost of changing them may exceed the benefit. What we would still fix is the duplication, for example by moving shared fragments into database views.

Options for a SQL-heavy JPA service
OptionFitsMain cost
Back to JPQLSimple filters, entity loadsWeak for reports; strings still duplicated
jOOQ for readsReports, DTO reads, vendor SQLCodegen in build, two styles, license for commercial DBs
Keep native SQLStable, well-tested queriesNo compile-time checks, duplication stays
JPA writes + jOOQ readsMixed workloadTeam must know both well

3Making the Decision With the Team

A senior engineer does not decide this from preference. We look at three things. Maintainability: how often do schema changes break native queries today? If the incident log shows several production failures a quarter from renamed columns, compile-time checking has a concrete value. Test coverage: if only 60 of the 200 queries have integration tests, any migration is risky, and the first investment is tests, whatever tool we pick. Team skill: a team strong in SQL will be productive in jOOQ within weeks; a team that mostly knows Spring Data may find it a steep change.

Suppose the evidence shows frequent drift breakages, weak tests and a SQL-literate team. Then a reasonable choice is to keep JPA for aggregate writes, where dirty checking, cascades and optimistic locking earn their keep, and adopt jOOQ for reads. Both can share the same DataSource and transaction manager, so a service method can write with JPA and read with jOOQ in one transaction. We would present this to the team with the trade-offs written out and invite objections, because the people maintaining both styles must believe in the choice.

Step throughDeciding what to do with the native queries
  1. Classify the 200 querieshabit filters, vendor SQL, reports
  2. Do schema changes break queries often?
    YesCompile-time checks have real valueNoKeeping them is defensible
  3. Are most queries covered by tests?
    YesSafe to migrate graduallyNoWrite tests first
  4. Is the team comfortable with SQL?
    YesjOOQ for reads, JPA for writesNoJPQL where it fits, views for shared parts
  5. Record the decision and the ruleADR plus a review date

5 steps in order. Play them, or step through them one at a time.

4Migrating Gradually

Whatever we choose, we do not rewrite 200 queries in one branch. We set up jOOQ code generation against a database migrated by our Flyway scripts, then move queries one repository at a time, starting with the ones that change most often or broke most recently. Each migrated query keeps its integration test, and the test asserts the same rows, in the same order, before and after.

The 20 habit queries move to derived queries or JPQL as they are touched. The shared fragments become jOOQ Condition methods, such as activeCustomer() returning a reusable where clause, which removes the duplication for good.

5Setting a Rule for Native SQL

The last step is what stops the drift returning. A short written rule might say: entity loads and writes use Spring Data JPA; reads that return DTOs or need vendor SQL use jOOQ; a raw native string is allowed only for something neither can express, and needs a comment explaining why and an integration test. Code review then has something to point to instead of a debate each time.

6Follow-up questions

Can JPA and jOOQ share one transaction?

Yes. In Spring Boot both use the same DataSource, and the jOOQ starter wires its connection provider to Spring's transaction management, so inside a @Transactional method they use the same connection. One catch is that JPA may hold unflushed changes, so we flush before a jOOQ read that must see them.

Why not move everything to jOOQ and drop JPA?

For aggregate writes, JPA gives us dirty checking, cascades, optimistic locking and lifecycle callbacks with little code. Rebuilding those with SQL is possible but costs effort and introduces bugs. Many teams find the split, JPA for writes and SQL-first for reads, gives the best of both.

What if the team has no time for a migration?

Then we at least stop the bleeding: add integration tests for the riskiest queries, move duplicated fragments into views, and adopt the rule for new code. That leaves the door open for a later migration and lowers today's risk at small cost.

7Conclusion

In this answer, we treated 200 native queries as evidence that the service's reads are SQL-centric. We compared moving back to JPQL, adopting jOOQ, and keeping the queries, saw that jOOQ adds compile-time checks and composition at the cost of codegen, a second style and licensing for commercial databases, and chose based on drift incidents, test coverage and team skill. A gradual migration with tests, and a written rule for when native SQL is acceptable, finish the job.

What to say in the interviewAbout 80 seconds aloud

Open with

First I would look at what the queries are. Two hundred native queries usually mean the service has become read-heavy and SQL-shaped, reports and DTO screens, so moving them back into JPQL would mostly translate strings into other strings and keep the copy-paste.

Then, if they want more

I would classify them: habit queries that could be derived or JPQL, vendor-specific ones, and reports.

Then I would compare the options with the team. jOOQ generates classes from the schema, so a renamed column fails compilation instead of production, and fragments become reusable Java methods; the costs are a codegen step, a second style next to JPA, and a paid license if we are on Oracle or SQL Server.

I would decide on evidence: how often schema drift has broken queries, how many queries have integration tests, and how comfortable the team is with SQL. A common outcome is JPA for aggregate writes and jOOQ for reads, sharing the same transaction.

We would migrate gradually, a repository at a time, with tests proving identical results, and write down a rule: native SQL only when neither tool can express the query, with a comment and a test.

About this answer

Last updated
12 November 2026
Versions it names
Hibernate 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.
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 "A service has grown to 200 native queries in JPA repositories, many with copy-pasted...": https://scrivio-interview-library.netlify.app/databases/jpa-queries/36f15444c7/