When Your H2 Schema Won't Change on Hibernate HBM2DDL Create

One of the great things about lightweight databases like H2 is that you are generating a fresh schema every time you launch your tests or application. That characteristic would suggest any changes you make to the schema would be seen on the next launch.

I discovered, however, that you cannot take that schema update for granted, as I faced the issue of my H2 Schema not changing no matter what I did. My Spring HBM2DDL property was properly set to “create”, yet app launch after launch and the schema remained the same. Rebuilding the Project in IntelliJ, Gradle Clean, invalidating IntelliJ Cache and restarting…nothing worked. On the other hand, any changes I made in my h2data.sql file were loading correctly, so I was stumped.

We're looking for changes in a table named post_category_ids, a simple lookup table to which we added a new post_category_id AUTO_INCREMENT primary key. This is the table script in the h2schema.sql file launched at startup with a Spring DatabasePopulator Bean.

Launch after launch and no post_category_id column was to be found.

Then I happened to review my MySQL database setup script where I saw a difference.

DROP TABLE IF EXISTS post_category_ids;

Surely the H2 table didn't already exist. I'm creating the database from scratch. I'm even saving to a physical file which I manually deleted after each run during the course of finding a solution.

Yet dropping the table first was the solution. Shouldn't be necessary and it makes no sense to me, but it was indeed the fix.

H2Console confirms we can finally get back to coding.

Posted January 21, 2017 06:16 PM EST

More Like This Post