Spring Boot
Database Schema
Java
ORM
JPA

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:

java
1import jakarta.persistence.Column;
2import jakarta.persistence.Entity;
3import jakarta.persistence.GeneratedValue;
4import jakarta.persistence.GenerationType;
5import jakarta.persistence.Id;
6import jakarta.persistence.Table;
7
8@Entity
9@Table(name = "users", schema = "app_core")
10public class UserEntity {
11
12    @Id
13    @GeneratedValue(strategy = GenerationType.IDENTITY)
14    private Long id;
15
16    @Column(nullable = false, unique = true, length = 120)
17    private String email;
18
19    @Column(name = "display_name", nullable = false, length = 80)
20    private String displayName;
21}

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:

yaml
1spring:
2  datasource:
3    url: jdbc:postgresql://localhost:5432/appdb
4    username: app
5    password: secret
6  jpa:
7    hibernate:
8      ddl-auto: validate
9    properties:
10      hibernate:
11        default_schema: app_core

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:

yaml
1spring:
2  flyway:
3    enabled: true
4    default-schema: app_core
5    schemas: app_core
6    locations: classpath:db/migration

Example migration:

sql
1CREATE SCHEMA IF NOT EXISTS app_core;
2
3CREATE TABLE IF NOT EXISTS app_core.users (
4    id BIGSERIAL PRIMARY KEY,
5    email VARCHAR(120) NOT NULL UNIQUE,
6    display_name VARCHAR(80) NOT NULL
7);

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:

java
1public interface UserRepository extends JpaRepository<UserEntity, Long> {
2
3    @Query(
4        value = "SELECT * FROM app_core.users WHERE email = :email",
5        nativeQuery = true
6    )
7    Optional<UserEntity> findNativeByEmail(@Param("email") String email);
8}

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.


Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design