JPA/Hibernate support for migration?

26,895

Solution 1

I usually let Hibernate generate the DDL during development and then create a manual SQL migration script when deploying to the test server (which I later use for UAT and live servers as well).

The DDL generation in Hibernate does not offer support for data migration at all, if you only do as much as adding a non-null field, DDL generation cannot help you.

I have yet to find any truely useful migration abstraction to help with this.

There are a number of libraries (have a look at this SO question for examples), but when you're doing something like splitting an existing entity into a hierarchy using joined inheritance, you're always back to plain SQL.

Solution 2

Maybe I'll have to introduce new entities, remove them or just change the types of properties in an entity.

I don't have any experience with it but Liquibase provides some Hibernate Integration and can compare your mappings against a database and generate the appropriate change log:

The LiquiBase-Hibernate integration records the database changes required by your current Hibernate mapping to a change log file which you can then inspect and modify as needed before executing.

Still looking for an opportunity to play with it and find some answers to my pending questions:

  • does it work when using annotations?
  • does it require an hibernate.cfg.xml file (although this wouldn't be a big impediment)?

Update: Ok, both questions are covered by Nathan Voxland in this response and the answers are:

  • yes it works when using annotations
  • yes it requires an hibernate.cfg.xml (for now)

Solution 3

There are two options:

  • db-to-hibernate - mirror DB changes to your entities manually. This means your DB is "leading"
  • hibernate-to-db - either use hibernate.hbm2ddl.auto=update, or manually change the DB after changing your entity - here your object model is "leading"
Share:
26,895
Rémi Vennereau
Author by

Rémi Vennereau

33 year old Swede living in London.

Updated on March 08, 2020

Comments

  • Rémi Vennereau
    Rémi Vennereau about 4 years

    I'm currently working on a desktop application using JPA/Hibernate to persist data in a H2 database. I'm curious what my options are if I need to make changes to the database schema in the future for some reason. Maybe I'll have to introduce new entities, remove them or just change the types of properties in an entity.

    • Is there support in JPA/Hibernate to do this?
    • Would I have to manually script a solution?