Scrivio Interview library
DebuggingIntermediate6 min read · updated 12 November 2026

A service takes 40 seconds to start, and profiling shows most of it inside Hibernate's metadata building and schema validation for 900 entities. What would you look at?

We would look at five things. Schema validation with ddl-auto=validate reads database metadata for every table, so in production we can use none and verify the schema with Flyway or Liquibase tests instead. Entity scanning over large packages can be narrowed with @EntityScan. Bytecode enhancement should happen at build time, not at startup. Spring Data's bootstrap-mode deferred or lazy lets the EntityManagerFactory build in the background while the rest of the context starts. And 900 entities in one persistence unit usually means a shared model that should be split.

Why this question is asked

Slow startup hurts more than it seems: deployments take longer, autoscaling reacts late, every test that starts the context is slow, and developers wait on each restart. When a profiler points at Hibernate's metadata building, the cause is rarely one setting; it is the combination of how much Hibernate is asked to model and what it is asked to check. Interviewers ask this to see whether we can break a vague slow startup into measurable parts. In this answer, we will see what Hibernate does at startup, measure it, and then go through the five places to look, from quick wins to the structural fix.

1What Hibernate Does at Startup

Building the EntityManagerFactory is a pipeline. Spring first scans packages for classes annotated with @Entity. Hibernate then builds its metadata model: for each entity it reads annotations, resolves types, associations, inheritance and naming, and prepares SQL strings for loading, inserting, updating and deleting. If ddl-auto is set, it then compares the model with the database or changes the schema. With 900 entities, every step is multiplied, and schema validation adds database round trips for every table, column and sequence.

Step throughWhere startup time goes
  1. Scan packages for @Entityclass path scanning
  2. Build metadata for 900 entitiesannotations, types, associations, SQL
  3. ddl-auto=validate?
    Yesread metadata for every table and columnNoskip the database check
  4. Create repositoriesquery methods parsed and checked
  5. Application ready

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

2Measuring Before Changing

We want numbers per phase, not one total. Spring Boot's startup tracking records how long each step and each bean took; with BufferingApplicationStartup and the Actuator startup endpoint we can see the time spent creating the entityManagerFactory bean. Hibernate's own debug logs show when metadata building and schema validation start and end.

Java
SpringApplication app = new SpringApplication(ShopApplication.class);
app.setApplicationStartup(new BufferingApplicationStartup(10_000));
app.run(args);
// GET /actuator/startup (example numbers)
// bean entityManagerFactory 31.8 s
// other beans 8.1 s
// Hibernate debug log timestamps split the 31.8 s:
// metadata building 19.5 s
// schema validation 11.2 s

With numbers like these, the two big targets are clear, and we can check each change against them.

3Schema Validation Against Every Table

With spring.jpa.hibernate.ddl-auto=validate, Hibernate reads the database's catalogue for every mapped table, column and sequence and compares types and existence. For 900 tables over a network connection that is a lot of metadata queries, and on some databases catalogue queries are slow.

Validation is valuable, but it does not have to run on every production start. Schema changes should already come from Flyway or Liquibase migrations. If the build runs a test that applies all migrations to a fresh database (with Testcontainers) and then starts Hibernate with validate, a mismatch is caught before deployment, and production can run with ddl-auto=none.

Properties
# production
spring.jpa.hibernate.ddl-auto=none
# integration test profile: migrations, then validation
spring.flyway.enabled=true
spring.jpa.hibernate.ddl-auto=validate
// a mismatch fails the build: Schema-validation: missing column [discount_code] in table [orders]

4Scanning, Enhancement and Bootstrap Mode

Entity scanning starts at the application's package. If that package is the root of a large shared library, Spring scans many classes that are not entities, and may even pick up entities the service never uses, which then add to metadata building. @EntityScan with the exact packages the service needs keeps both lists small.

Bytecode enhancement, if used, should run at build time with the Hibernate Maven or Gradle plugin. Runtime enhancement through a class transformer adds work while classes load, which is avoidable. We check the build for the plugin and the configuration for runtime enhancement settings.

Finally, Spring Data's spring.data.jpa.repositories.bootstrap-mode can be set to deferred or lazy. With deferred, Spring Boot builds the EntityManagerFactory on a background thread while the rest of the context starts, and repositories are initialised once the context is ready; with lazy, repositories are created only on first use. This hides part of the 30 seconds behind other startup work rather than removing it, so it is a mitigation, not a cure.

Java
@EntityScan("com.acme.billing.model") // only the packages this service owns
spring.data.jpa.repositories.bootstrap-mode=deferred
# the EntityManagerFactory builds in the background; startup overlaps with other beans

5The Structural Question: Why 900 Entities?

A single service rarely needs 900 entities. Usually there is a shared model library that every service depends on, so each service maps the whole company's database whether it uses ten tables or all of them. That is the real cause of the cost, and it also couples every service to every schema change.

The lasting fix is to split the model: each service maps only the tables it owns, and shared data is read through APIs or events, or through small read-only mappings. If the application genuinely spans several domains, separate persistence units per domain at least let each one build and validate independently. Going from 900 entities to 120 shrinks metadata building roughly in proportion, and makes the earlier fixes almost unnecessary.

6Follow-up questions

Is turning off validation in production risky?

Only if nothing else checks the schema. With migrations tested against a fresh database and a validation test in the build, the risk is covered earlier and more reliably than at production startup, where a failure means an outage anyway.

Would spring.main.lazy-initialization=true help?

It postpones creating beans until they are first used, which shortens the startup log but moves the cost to the first requests. For the EntityManagerFactory, bootstrap-mode deferred is a more targeted option, and neither replaces reducing the work itself.

Can we cache Hibernate's metadata between starts?

Not in a supported way for a typical Spring Boot service. The better levers are mapping fewer entities, skipping repeated validation, and, where startup time is critical, ahead-of-time options such as class data sharing or native images, whose support for Hibernate you should check for your version.

7Conclusion

In this answer, we broke the 40-second startup into scanning, metadata building and schema validation, and measured each with Spring Boot's startup tracking. We then replaced per-start validation with migration tests and ddl-auto=none, narrowed scanning with @EntityScan, moved enhancement to the build, let the EntityManagerFactory build in the background with bootstrap-mode deferred, and addressed the root cause: 900 entities in one persistence unit usually means a shared model that should be split.

What to say in the interviewAbout 90 seconds aloud

Open with

First I would measure the phases with Spring Boot's startup tracking, BufferingApplicationStartup and the Actuator startup endpoint, plus Hibernate debug logs, so I know how much is metadata building and how much is schema validation.

Then, if they want more

Then I would look at five things. One, ddl-auto validate: it reads database metadata for every table, column and sequence, which for 900 tables is many catalogue queries. I would move that check into the build, applying the Flyway or Liquibase migrations to a fresh database in a test and validating there, and run production with none.

Two, entity scanning: if the application package is the root of a big shared library, Spring scans far more than needed and may map entities the service never uses, so I would restrict it with EntityScan. Three, bytecode enhancement should be done at build time with the Hibernate plugin, not at runtime.

Four, Spring Data's bootstrap-mode deferred makes Boot build the EntityManagerFactory in the background while the rest of the context starts, which hides some of the time. But the real question is why one service has 900 entities. That almost always means a shared domain model that every service maps entirely.

The lasting fix is to split it so each service maps only the tables it owns, or at least separate persistence units per domain. Fewer entities shrink metadata building in proportion.

About this answer

Last updated
12 November 2026
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 takes 40 seconds to start, and profiling shows most of it inside Hibernate's...": https://scrivio-interview-library.netlify.app/databases/jpa-fundamentals/c92cfcbf87/