Video in progress
How do you plan moving Hibernate-based services from one database to another (say, from Oracle to PostgreSQL)?
Hibernate gives you only part of the portability: JPQL and mappings carry over, but native SQL, sequence/identity, types (NUMBER, CLOB, dates), case sensitivity and transaction behavior do not. The plan: an inventory, tests against both databases, data migration (kept in sync with CDC), gradual traffic cutover and a way back.
📖 Answer
The first step is to list what depends on the database. The list usually looks like this:
- native queries and the SQL inside
@Formula,@SQLRestriction,@ColumnTransformer; - stored procedures, triggers, views, packages (a lot of these on Oracle);
- id generation: Oracle sequences, their
INCREMENT BYandallocationSize; - types:
NUMBER(1)as a boolean,DATE(which includes a time on Oracle),CLOB/BLOBvstext/bytea, the empty string (on Oracle''=NULL!); - identifier case and name length limits;
- isolation and lock behavior (
for update nowait,skip lockedsyntax).
On the Hibernate side, switching the dialect is easy by itself; the hard part is everything above. I cover every native query with a test that runs on PostgreSQL in Testcontainers, then run the test suite on both databases in parallel.
Data migration: an initial full copy (ora2pg or a similar tool), then keep the changes in sync with CDC (Debezium or a vendor tool). At the very end, set the sequence values in the new database above the old maximum: people often forget this.
Cutover: first move part of the read traffic to the new database (comparing the results, a "shadow read"), then move writes with a short write window. To keep a way back, reverse CDC (new → old) keeps running for a while.
Trade-offs: running two databases in parallel is expensive and complex, but a "big bang" migration is even riskier. If each service can be moved separately, I start with the least risky one and build up experience. Before the move I also compare run times: a query that was fast on Oracle can be slow on PostgreSQL because of a different plan.
Sign up to mark studied questions and save your progress.