๐ด The Error You're Seeing
Confirm this matches your console output. If it does, you're in the right place.
2023-10-25 10:15:30.812 ERROR 12345 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute statement; SQL [n/a]] with root cause
org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "USER" not found; SQL statement:
insert into user (email, name, id) values (?, ?, ?) [42102-214]โก Quick Fix Works 80% of the time
Set spring.jpa.hibernate.ddl-auto=update to let Hibernate create the tables automatically.
# application.properties
spring.jpa.hibernate.ddl-auto=update๐ ๏ธ Solutions (3 Ways to Fix)
Enable Hibernate auto-schema generation
๐ Use this during development when your Entity classes exist but the database tables haven't been created.
Setting ddl-auto to 'update' tells Hibernate to scan your @Entity classes and automatically create or alter the database tables to match.
# application.properties
spring.jpa.hibernate.ddl-auto=updateFix @Table name mismatch
๐ Use this if your table exists in the DB but your Entity is looking for the wrong name.
Hibernate defaults to the class name for the table name. If your DB table is 'users' but your class is 'User', it will fail. Use @Table to map them explicitly.
@Entity
@Table(name = "users")
public class User { ... }Initialize database with data.sql
๐ Use this if you have a custom schema.sql file that isn't running.
Ensure your schema.sql file is in src/main/resources and that Spring Boot is configured to run it.
# application.properties
spring.sql.init.mode=always
spring.jpa.defer-datasource-initialization=true๐ Version Notes
spring.sql.init.mode defaults to embedded. schema.sql runs automatically for H2.
spring.sql.init.mode defaults to embedded, but defer-datasource-initialization is needed if using Hibernate ddl-auto with schema.sql.
๐ง Why this Happens
Tap to expand the deep technical explanation
Hibernate generated a SQL query (like SELECT or INSERT) for an Entity, but when it sent that query to the database, the database responded that the table doesn't exist. This usually means your Entity classes haven't been mapped to actual database tables yet.
The HITEC City Parking Spot Analogy:
It's like trying to put a letter in a mailbox, but the mailbox hasn't been built yet. You have the letter (data), but nowhere to put it.
๐ก๏ธ How to Prevent This Next Time
Always set spring.jpa.hibernate.ddl-auto=update during development, or use Flyway/Liquibase for production schema management.