Hibernate fails on a CREATE or ALTER TABLE statement at startup
Error executing DDL "alter table events drop foreign key FKg0mkvgsqn8584qoql6a2rxheq" via JDBC StatementShort answer
Hibernate generated a CREATE, ALTER or DROP statement and MySQL refused it. The useful text is the SQL error under the message. Three causes cover most cases: an entity or column name that is a reserved word, a foreign key whose column types or engine do not match, or ddl-auto running against a schema you manage yourself.
Fix it fast
1Find out which cause you have
Scroll to the 'Caused by' line under the DDL message: it holds MySQL's own error and the exact statement that failed. Copy that statement and run it by hand in the mysql client.
grep -n -A6 'Error executing DDL' app.log | head -n 30 mysql -u app_user -p appdb2Match what you see, then jump to its fix
- 1
The SQL error is a syntax error near a name such as order, group, key or rank in a create table statementA table or column name is a reserved wordFix - 2
The failing statement is an alter table ... add constraint ... foreign keyA foreign key does not match its parent column or engineFix - 3
Errors on drop or alter statements for tables and constraints that already exist or already differddl-auto is changing a schema that you manage yourselfFix
- 1
What does the error look like?
Error executing DDL "alter table events drop foreign key FKg0mkvgsqn8584qoql6a2rxheq" via JDBC Statement
Cause 1 of 3
A table or column name is a reserved word
1Check
MySQL only allows a reserved word as an identifier when it is quoted with backticks. ORDER, GROUP, KEY, RANK and ROWS are reserved in MySQL 8.4, while USER is not. A table or column given one of those names needs quoting or a new name.
grep -rn '@Table\|@Entity' src/main/java | head
grep -rn 'ddl-auto' src/main/resources/2Fix
Rename the table or column in the mapping. If renaming is not an option, ask Hibernate to quote every identifier.
@Entity
@Table(name = "customer_order")
public class Order { }
# or, in application.properties
spring.jpa.properties.hibernate.globally_quoted_identifiers=true3Verify
Restart the app and check that the create table statement now succeeds.
mysql -u app_user -p appdb -e 'SHOW TABLES;'Confirmed by dev.mysql.com
Cause 2 of 3
A foreign key does not match its parent column or engine
1Check
MySQL requires parent and child tables to use the same storage engine, columns of similar type with the same size and sign, and an index on the referenced column. An INT parent key and a BIGINT child column, or an old MyISAM table, break it.
SHOW CREATE TABLE parent;
SHOW CREATE TABLE child;2Fix
Align the column types (change the Java field type or the column definition) and use InnoDB for both tables. Drop a stale foreign key by its real name, which SHOW CREATE TABLE prints.
ALTER TABLE child MODIFY parent_id BIGINT NOT NULL;
ALTER TABLE child DROP FOREIGN KEY child_ibfk_1;3Verify
Run the app again, or add the constraint by hand and see it succeed.
mysql -u app_user -p appdb -e 'SHOW CREATE TABLE child\G'Confirmed by dev.mysql.com
Cause 3 of 3
ddl-auto is changing a schema that you manage yourself
1Check
Spring Boot defaults ddl-auto to create-drop only for an embedded database with no Flyway or Liquibase. If you set create, create-drop or update on a real MySQL database, Hibernate tries to drop and alter objects it did not build.
grep -rn 'ddl-auto' src/main/resources/ config/2Fix
Set ddl-auto to none, or validate to only compare entities with the tables, and manage the schema with scripts or a migration tool.
spring.jpa.hibernate.ddl-auto=validate3Verify
Restart; the log shows no DDL being executed.
grep -c 'Error executing DDL' app.logConfirmed by docs.spring.io
How do I stop it coming back?
- Pick table and column names that are not MySQL reserved words.
- Use none or validate outside local development, and keep the schema in migrations.
- Create parent and child key columns with identical types.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- Dispatch ProA real-time logistics platform: Flutter apps, Spring Boot services and a Kafka event pipeline on AWS.
Related fixes
Databases
Java cannot log in to MySQL 8: authentication plugin caching_sha2_password not supported
ERROR 2061 (HY000): Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection.
MySQL 8 accounts use the caching_sha2_password plugin by default, and Connector/J releases before 8.0.9 cannot use it.
3 causes, each with a fixDatabases
Spring Boot stops with 'Unable to build Hibernate SessionFactory'
Could not open JPA EntityManager for transaction
This message only says that JPA could not start.
3 causes, each with a fixDatabases
@CreatedDate is not filled in when I save an entity
@CreatedDate and @LastModifiedDate fields stay null
Spring Data JPA fills @CreatedDate only when auditing is switched on and the entity is wired to the auditing listener.
4 causes, each with a fixDatabases
H2 in PostgreSQL mode still behaves differently from PostgreSQL
Names or null ordering differ from PostgreSQL in test results
H2 compatibility modes are switched on in the JDBC URL and cover only a small part of the differences between databases.
4 causes, each with a fixDatabases
EntityNotFoundException when loading an entity that has a missing related row
ConstraintViolationException on an orphaned foreign key
EntityNotFoundException means Hibernate followed an id and no row was there.
3 causes, each with a fixDatabases
Hibernate cannot initialize a lazy collection or proxy in Spring Boot
could not initialize proxy - no Session
A lazy collection or proxy is loaded on first access, and that needs an open Hibernate session.
3 causes, each with a fix