๐Ÿ”ด The Error You're Seeing

Confirm this matches your console output. If it does, you're in the right place.

ERROR LOG2026-02-18 13:15:20.100 ERROR 8842 --- [ main] o.s.boot.SpringApplication : Application run failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' ... nested exception is org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing table [users] OR org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "USERS" already exists; SQL statement: CREATE TABLE users (id BIGINT NOT NULL, name VARCHAR(255), PRIMARY KEY (id)) [42101-214]

โšก Quick Fix Works 80% of the time

Change spring.jpa.hibernate.ddl-auto to 'update' or 'none' instead of 'create'.

# application.properties spring.jpa.hibernate.ddl-auto=update

๐Ÿง  Why this Happens

Tap to expand the deep technical explanation

You have configured Hibernate to generate tables (e.g., `ddl-auto=create` or `create-drop`), but you also have a `schema.sql` file in your `src/main/resources` that tries to create the exact same table. The database rejects the second creation attempt.

The HITEC City Parking Spot Analogy:

It's like a builder laying the foundation for a house, but the architect also hired a second builder to lay the exact same foundation. The second builder hits concrete and says, 'This already exists!'

๐Ÿ” How to Reproduce Confirm this is your error

Add a `schema.sql` file with `CREATE TABLE users (...);`. Set `spring.jpa.hibernate.ddl-auto=create`. Run the Spring Boot app with an H2 database.

๐Ÿ› ๏ธ Solutions (5 Ways to Fix)

Solution 1โœ“ Most common cause

Change ddl-auto to 'update'

๐Ÿ‘‰ Use this as the general development setting.

Hibernate will only create the table if it doesn't exist, and will alter it if the Entity changes. It won't try to recreate it every time.

# application.properties spring.jpa.hibernate.ddl-auto=update
Solution 2

Disable schema.sql execution

๐Ÿ‘‰ Use this if you want Hibernate to handle all table creation and you don't need `schema.sql`.

Tell Spring Boot not to run your SQL initialization scripts.

# application.properties spring.sql.init.mode=never
Solution 3

Use 'create-drop' ONLY for fresh tests

๐Ÿ‘‰ Use this in `application-test.properties` for integration tests.

`create-drop` drops tables on shutdown and recreates them on startup. It ensures a clean slate but fails if `schema.sql` also runs.

# src/test/resources/application-test.properties spring.jpa.hibernate.ddl-auto=create-drop spring.sql.init.mode=never
Solution 4

Add 'IF NOT EXISTS' to schema.sql

๐Ÿ‘‰ Use this if you must use both Hibernate and `schema.sql`.

Modify your SQL script to ignore errors if the table is already present.

-- src/main/resources/schema.sql CREATE TABLE IF NOT EXISTS users ( id BIGINT PRIMARY KEY, name VARCHAR(255) );
Solution 5

Defer DataSource Initialization

๐Ÿ‘‰ Use this if you are on Spring Boot 3 and `schema.sql` runs before Hibernate creates tables.

In Spring Boot 3, `schema.sql` runs before Hibernate by default. This property reverses the order so Hibernate creates the tables first, then `schema.sql` inserts data.

# application.properties spring.jpa.defer-datasource-initialization=true

๐Ÿ“‹ Version Notes

Spring Boot 2.x

`schema.sql` runs before Hibernate. `spring.sql.init.mode` defaults to `embedded`.

Spring Boot 3.x

Requires `spring.jpa.defer-datasource-initialization=true` if using `schema.sql` with `ddl-auto`.

๐Ÿ›ก๏ธ How to Prevent This Next Time

Choose ONE source of truth for your schema. Either let Hibernate manage it (`ddl-auto=update`) OR use Flyway/Liquibase. Don't mix `ddl-auto=create` with `schema.sql`.

Course Search
Search across all chapters & stages
๐Ÿ“–

Search the course

Type any topic โ€” branching, stash, rebase, hooks โ€” and jump straight to that chapter.

merge branchesgit stashundo commitrebase