How to properly specify the database schema in Spring Boot
Codemia Knowledge Hub ·
Pick one owner for schema changes (usually Flyway or Liquibase), then map entities to the schema with @Table(schema = ...) or a global hibernate.default_schema, and qualify native queries explicitly. Schema handling gets messy when entity mappings, Hibernate defaults and migration tools all try to control the same thing.
Decide who owns schema changes
Schema evolution should be owned by one of:
- Hibernate auto-DDL, suitable mainly for prototypes
- Flyway or Liquibase, the usual production choice
If Hibernate and a migration tool both modify the schema, drift and surprise changes become likely. A common production pattern is:
- migrations create and evolve the schema
- Hibernate validates mappings against it
Avoid ddl-auto=create or update in shared environments.
Map the schema on the entity
If an entity belongs to a specific schema, say so explicitly:
This avoids relying on database default search paths, which can differ across environments. Relying on the database user's default schema is a common source of bugs.
Configure a default schema
If most entities live in the same schema, set it globally:
ddl-auto: validate fits when migrations own the real schema changes and Hibernate should only confirm that the mappings still match. Use hibernate.default_schema only when a true default exists for most entities.
Keep migrations schema-aware
If you use Flyway, configure the schema explicitly:
Example migration:
This makes the schema history reproducible instead of depending on whichever default schema happens to be active.
Qualify the schema in native queries
Entity annotations cover JPA-managed operations, but native SQL needs its own schema handling. When in doubt, qualify the schema:
If the application works through entities but fails through native queries, missing schema qualification is a common reason.
Applications that span several schemas
Some services legitimately use multiple schemas, such as an operational schema and an audit schema. In that case:
- specify
schema = ...per entity - organize migrations by schema or module
- do not rely on one global default to cover everything
If the boundaries are strong enough, separate datasources or persistence units may be cleaner than one large mixed schema model.
Test against the same database family
Schema behavior is one place where production and local databases diverge painfully. When schema qualification matters, run integration tests against the same database family you use in production. A PostgreSQL-specific default schema assumption, for example, may not fail visibly if the test database is too different.
Related reading
- How to pull a random record using Django's ORM?
- How to pull in Heroku postgres credentials for next-auth?
- How to put data from MySQL to Kafka producer?
- How to query as GROUP BY in Django?
- How to properly stop the Thread in Java?
- How to proxy a HTTP video stream to any amount of clients through a Spring Webserver
- How to query AWS DynamoDb using KeyConditionExpression?
- How to query AWS DynamoDB using multiple Indexes?
System design interview problems on this topic
- Design a Distributed OLTP Database: solution and walkthrough
- Design a Wide Column Database: solution and walkthrough
- Design a Key Value Store: solution and walkthrough
- Design a Web Cache: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.