Play 2.2.X + JPA + Hibernate – Bootstrapping the Database migration

Play Logo

Hibernate logo


After reading about the issue of migrating databases on apps based on Play 2.2.X with JPA + Hibernate here: and here:

I actually ended up using Flyway and it’s Play extension/plugin to do the DB migrations.

But inspired on other technologies that I used before, and on a comment I saw somewhere on StackOverflow (sorry, no link, I cannot remember where I saw it), I decided to make my life easier by using Hibernate’s automatic schema migration features to bootstrap my migration script.

After a bit of googling I knew that it was possible to do it programmatically or with an SBT/Maven/whatever plugin because of this:

After briefly considering, and giving up on the idea of making a build ‘target’ (call it target, goal or whatever depending on how you build your app). I gave up on the idea because I knew very little about SBT, and I didn’t want to migrate the Play app to Maven, just for the sake of doing this.

So, inspired by this page I wrote the following Plugin instead:

As you can see, it writes the migration script to the “conf/db/migration” + <persistenceUnitName> + “.sql”  file, that can then be used as a base to write your own migration script.

After writing that plugin, I simply had to enable it by adding it to the play.plugins file, after the flyway one (to allow the migration to occur):


I also commented the

<property name="" value="validate" />

line at the persistence.xml file, as the ‘validation’ is now done with the plugin.

If you have any suggestions/comments please feel free to share

Leave a Reply

Your email address will not be published. Required fields are marked *