Scrivio Interview library
ScenarioPrincipal6 min read · updated 12 November 2026

Your services use many database-specific native queries. The company is considering a move to a different database engine for cost reasons. How do you estimate how locked in you are and what the move would cost?

We measure lock-in by inventorying, per service, every native query, database function, hint, stored procedure and engine-specific data type, then classifying each item as portable as-is, rewritable in JPQL, or needing an engine-specific redesign. Test coverage of the data access layer tells us how risky each part is, performance validation on the new engine is a separate cost because query plans differ, and the total migration cost is then compared with the expected savings over several years.

Why this question is asked

Database licences and managed database bills are among the largest line items in many infrastructure budgets, so sooner or later someone asks whether moving to a cheaper engine would pay off. The people asking usually see the licence saving clearly and the migration cost not at all. Interviewers ask this scenario to see whether a senior engineer can turn a vague worry about lock-in into an estimate leaders can act on. In this answer, we will build the inventory, classify what we find, weigh test coverage and performance, and put the numbers into a multi-year comparison.

1What Lock-In Means Here

Lock-in is the cost of leaving. JPA reduces it for the code it generates, because Hibernate translates JPQL into each dialect. Everything we wrote ourselves in the engine's own language is not translated: native SQL strings, calls to database functions such as PostgreSQL's jsonb_path_query or Oracle's CONNECT BY, optimizer hints, stored procedures, triggers, sequences with special options, and column types such as jsonb, arrays, or Oracle's NUMBER semantics.

So the question is not whether we are locked in, because every real system is a little. The question is how much work it would take to remove each dependency, and how sure we are about that number.

2Building the Inventory

We start with facts, per service. A script can find most of them: search repositories for nativeQuery = true, @Procedure, @NamedNativeQuery, createNativeQuery, StoredProcedureQuery and @QueryHints; list column types from the information schema; list functions, procedures, triggers and views from the database catalog. Runtime data fills the gaps: pg_stat_statements, or the equivalent statistics view on our engine, shows which SQL actually runs and how often, including SQL built dynamically that a code search misses.

SQL
-- Database objects that are pure engine code (PostgreSQL catalog)
select n.nspname as schema, p.proname as name, p.prokind as kind -- f function, p procedure
from pg_proc p join pg_namespace n on n.oid = p.pronamespace
where n.nspname not in ('pg_catalog', 'information_schema');
-- Columns whose types will not map one to one
select table_name, column_name, data_type
from information_schema.columns
where data_type in ('jsonb', 'ARRAY', 'tsvector', 'uuid', 'interval');

Notice that we count usage too. A native query that runs a million times a day and one that runs once a month for an audit report carry very different risk, even if the rewrite effort is the same.

3Classifying Each Item

Each item goes into one of three buckets. Portable as-is: standard SQL that the target engine accepts unchanged, which is often more than people expect. Rewritable in JPQL or HQL: queries that went native out of habit, or that Hibernate 6 can now express, such as window functions. Needing redesign: things the target engine lacks or does differently, for example a jsonb-heavy data model moving to an engine with weaker JSON support, a recursive procedure, or logic that depends on PostgreSQL's transactional DDL.

We then give each bucket an effort per item. Portable items need a test run; rewrites take perhaps half a day each including tests; redesigns are estimated individually and may take weeks. Summed per service, this turns lock-in into a figure in engineer-days.

Example inventory for one service
ItemCountBucketEffort
Native selects, standard SQL48Portable as-isTest only
Native selects with jsonb operators22Redesign2 to 5 days each
Habit native queries30Rewrite in JPQLHalf a day each
Stored procedures9Redesign or port1 to 3 weeks each
Optimizer hints6Remove or replace1 day each, plus tuning
jsonb and array columns14Data model changePart of the redesign

4Test Coverage Decides the Risk

An estimate is only as good as our ability to verify the result. If a native query has an integration test that runs against the real database and checks its rows, we can run the same test against the target engine and know within minutes whether it works. If it has no test, we do not know what correct means, and the rewrite becomes archaeology.

So we measure data access coverage per service: which repositories and queries are executed by tests running on a real engine, for example through Testcontainers. Untested queries get a risk multiplier in the estimate, or better, we plan a first phase that writes the missing tests on the current engine. That phase pays off even if the move is cancelled.

5Performance Is a Separate Line Item

Correct results are not enough. Each engine has its own optimizer, so the same query can choose a different plan: a hash join instead of a nested loop, or a full scan because a functional index has no equivalent. A report that takes 200 milliseconds today can take 20 seconds after the move.

We therefore budget a performance validation phase: capture the top queries by total time from the statistics view, replay them against the target engine with production-like data volumes, compare plans and timings, and tune indexes. For a busy service this is often weeks, not days, and it must be in the estimate.

6Comparing Cost With Savings

Finally we put it together. Suppose the inventory across ten services comes to 600 engineer-days of rewrite, testing and tuning, plus a data migration with dual running for two months. At a loaded cost of, say, 800 per engineer-day, that is about 480,000, plus perhaps 100,000 for parallel infrastructure. If the new engine saves 150,000 a year, the move pays back in about four years, before counting the risk of incidents and the features delayed while engineers migrate.

Presented this way, leaders can make a real decision. Sometimes the answer is to move only new services, or to negotiate the licence with a credible migration estimate in hand, which is a saving in itself.

Step throughFrom inventory to a decision
  1. Inventory per servicequeries, functions, hints, procedures, types
  2. Classify each itemportable, rewrite, redesign
  3. Weight by test coverageuntested means higher risk
  4. Add performance validationreplay top queries on the target
  5. Savings over 3 to 5 years exceed cost?
    YesPlan a phased moveNoStay, or move only new services

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

7Follow-up questions

How do we find SQL built dynamically at runtime?

A code search finds string literals but misses SQL assembled from fragments or produced by libraries. Runtime statistics such as pg_stat_statements record the normalized text of every statement executed, so a few weeks of production data show what really runs. Combining both sources gives a far more complete inventory.

Does using JPA everywhere make us portable?

It helps a lot, because JPQL and derived queries follow the dialect. But schema details still leak through: column types, sequences, case sensitivity of identifiers, collation, and default transaction isolation differ between engines. Those need checking even in a service with no native SQL at all.

What is a cheap first step if the decision is not yet made?

Write integration tests on the current engine for every native query and procedure call. They reduce risk today, make the estimate reliable, and are the tool we would use to verify a migration later. Tagging native queries with a comment naming the engine feature they use also makes the next inventory faster.

8Conclusion

In this answer, we turned lock-in into an estimate. We inventoried native queries, functions, hints, procedures and data types per service, classified each as portable, rewritable or needing redesign, weighted the risk by test coverage, added performance validation because query plans differ between engines, and compared the full migration cost with the savings over several years so leaders can decide on numbers rather than hopes.

What to say in the interviewAbout 80 seconds aloud

Open with

I would turn the worry into an estimate. Per service, I would inventory everything that is engine-specific: native queries, database functions, hints, stored procedures, triggers and column types like jsonb or arrays.

Then, if they want more

I would find them with a code search and with runtime statistics such as pg_stat_statements, which also tell me how often each one runs. Then I would classify each item: portable as-is, rewritable in JPQL, or needing an engine-specific redesign, and attach an effort to each bucket.

Test coverage is the next thing I measure, because a query with an integration test on the real engine can be verified on the new one in minutes, while an untested query is the riskiest part of any move. If coverage is weak, the first phase is writing those tests, which pays off either way.

I would add a separate performance validation phase, replaying the top queries with production-like data, because query plans differ between engines. Finally I would add it all up in engineer-days and infrastructure cost and compare it with the expected savings over three to five years, so leadership sees the payback period and the risks.

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 "Your services use many database-specific native queries. The company is considering a...": https://scrivio-interview-library.netlify.app/databases/jpa-queries/7b4bc963d8/