Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 39 ยท Configuration and Sessions

Schema Generation and Naming Strategies

Learn what hibernate.hbm2ddl.auto and Jakarta schema generation do, why update is risky in production, how migrations help, and how naming strategies map names to snake_case.

Hibernate can create database tables for you from your entity mappings. That is fantastic in the first hour of a project and a trap in the first month of production. This lesson explains the schema-generation settings, what each value really does, why teams switch to migration tools, and how naming strategies decide which table and column names Hibernate produces.

The Hibernate settings are read-only here. The runnable programs are stand-ins: a naming strategy implemented in plain Java, and a "validate" check that compares an expected schema with the real H2 metadata.

hbm2ddl.auto

The Hibernate setting hibernate.hbm2ddl.auto (also available in Spring Boot as spring.jpa.hibernate.ddl-auto) selects what happens to the schema when the SessionFactory starts:

Value Effect Use for
none Do nothing Production
validate Check that tables and columns match the mappings; fail at start-up if not Production, staging, CI
update Add missing tables and columns Quick local experiments only
create Drop and recreate the schema at start Tests
create-drop create, then drop on SessionFactory close Tests, demos

update looks convenient, but it cannot drop or rename columns, cannot change types safely, cannot move data, and gives you no record of what changed. Different environments end up with different schemas. Use it on a throwaway local database or not at all.

There is also the standard (portable) setting, with the same idea:

XML
<property name="jakarta.persistence.schema-generation.database.action">drop-and-create</property>

Values are none, create, drop-and-create and drop. The standard settings can also write the DDL to a script file instead of running it:

XML
<property name="jakarta.persistence.schema-generation.scripts.action">create</property>
<property name="jakarta.persistence.schema-generation.scripts.create-target">schema.sql</property>

That is a neat way to see what Hibernate would run, and to bootstrap a first migration file.

Why migrations beat auto-DDL

Real databases evolve. A migration tool (Flyway or Liquibase) keeps versioned scripts in source control: V1__create_book.sql, V2__add_isbn.sql. Each environment applies the scripts in order and records what it ran. You review every schema change like code. Hibernate's role then shrinks to validate: it checks that the entities and the migrated schema agree. See Flyway migrations.

flowchart LR
    A["Entity change"] --> B["Write migration<br/>V3__add_isbn.sql"]
    B --> C["Flyway applies it on deploy"]
    C --> D["Hibernate validate<br/>checks the match"]

Migrations change the schema; Hibernate only verifies.

You can also ask Hibernate to do the checks programmatically on demand: in Hibernate 6+ sessionFactory.getSchemaManager() exposes validate, create, drop and truncate operations, which are handy in tests.

Naming strategies

If you write @Entity class OrderLine { private String productName; }, what are the table and column called? Two strategies work together:

  • The implicit naming strategy decides the name when you did not write @Table or @Column. The default (JPA-compliant) gives OrderLine and productName.
  • The physical naming strategy is applied to every name, implicit or explicit, last. It is where you enforce the database's conventions, e.g. snake_case and lowercase.

Spring Boot ships a physical strategy that converts orderLine to order_line, so most Spring projects get snake_case tables without any extra work. In plain Hibernate you opt in:

Java
.setProperty("hibernate.physical_naming_strategy",
             "org.hibernate.boot.model.naming.CamelCaseToUnderscoresNamingStrategy")

Explicit names are never changed by the implicit strategy, but they do pass through the physical strategy, so @Column(name = "productName") would also become product_name with the strategy above.

Stand-in: a naming strategy

This stand-in implements the camelCase-to-snake_case rule and applies it to a few attribute names. Real Hibernate does the same kind of transformation on every table, column, join column and sequence name.

Java Stand-in: camelCase to snake_case

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Stand-in: what validate does

validate reads the live schema through JDBC metadata and compares it with the mappings. If a table or column is missing, start-up fails with a message instead of failing later in a query. This stand-in implements the check on H2 for one expected table.

Java Stand-in: validate the schema

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

A sensible setup

  • Local development: create-drop or update on an in-memory or disposable database, with an import.sql or test data loader.
  • Tests: create-drop on H2 or Testcontainers, so each test run starts clean.
  • Staging and production: none or validate, with Flyway or Liquibase owning the schema.
  • Pick the naming strategy once, at the start. Changing it later renames every table and column.
  • Remember that mapping annotations such as @Column(length = 100, nullable = false) influence the generated DDL; with migrations you must keep the SQL and the annotations in step by hand, and validate catches missing items (but not every length or constraint difference).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which hbm2ddl.auto value only checks the schema and fails if it does not match?

    validate compares the mappings with the existing schema and fails at start-up on a mismatch; it changes nothing.
  2. 2. Why is update risky in production?

    Auto-update is incomplete and unreviewable; migration scripts are the safe way.
  3. 3. What does the physical naming strategy do?

    It transforms all names, implicit or explicit, for example to snake_case.
  4. 4. A common production setup is:

    Versioned migrations manage the schema; Hibernate validates it.

Summary

  • hbm2ddl.auto (or the Jakarta schema-generation settings) creates, updates or validates the schema at start-up.
  • create-drop suits tests; validate or none suits production; update suits almost nothing.
  • Use Flyway or Liquibase for real schema evolution; keep Hibernate as the checker.
  • The implicit naming strategy names unnamed things, the physical strategy rewrites all names (for example snake_case).
  • Choose naming conventions early; changing them later renames the whole schema.

Next: Bootstrapping Hibernate in Code