Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA/Hibernate support for migration?

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?
like image 481
willcodejavaforfood Avatar asked Oct 13 '10 12:10

willcodejavaforfood


People also ask

Is database migration easy in Hibernate?

With Hibernate it may be possible to use the same code on different databases (in practise it is probably more difficult partly because of the issues you mentioned (e.g.stored procedures). this cannot automatically done by Hibernate).

What is DB migration in spring boot?

Spring Boot makes this integration very simple. You only need to add a dependency for Liquibase or Flyway and put a description of your database update operations in the default folder. Spring Boot then provides a default configuration and triggers the migration.


3 Answers

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.

like image 84
Henning Avatar answered Oct 13 '22 06:10

Henning


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)
like image 33
Pascal Thivent Avatar answered Oct 13 '22 06:10

Pascal Thivent


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"
like image 44
Bozho Avatar answered Oct 13 '22 05:10

Bozho