32

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?
vbence
  • 20,084
  • 9
  • 69
  • 118
willcodejavaforfood
  • 43,223
  • 17
  • 81
  • 111

3 Answers3

24

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.

Community
  • 1
  • 1
Henning
  • 16,063
  • 3
  • 51
  • 65
  • This is an old question, but it keeps being asked, one way or another. I'll mention http://mybatis.org/migrations-maven-plugin/usage.html here, for future reference. It allows database maintenance/versioning via SQL - this allows data migration as well. From the existing/maintained database you can then generate the JPA entities using hibernate. – user625488 Jan 21 '20 at 17:05
  • hey whats the need of migration tool if ,we can create database and stuffs using hibernate-jpa @user625488 – Ishan Garg Mar 22 '21 at 13:07
  • @IshanGarg sometimes we need to make a change in a database, helps to make it seamless. For Example when adding / removing a new table, you wouldn't want to drop the whole database in order to do this. So migrations help you integrate this into a running database seamlessly. – was1209 Sep 24 '21 at 12:22
8

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)
Community
  • 1
  • 1
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • Cool, I don't actually have a hibernate mapping file, just a persistence.xml – willcodejavaforfood Oct 13 '10 at 15:48
  • @willcodejavaforfood Yeah, same here. But as I wrote, I'm not sure it can deal with a persistence.xml (I was thinking about generating an hibernate.cfg.xml to play with it if required). Maybe open another question and [Nathan Voxland](http://stackoverflow.com/users/45756/nathan-voxland) (the creator of Liquibase) will show up :) – Pascal Thivent Oct 13 '10 at 16:17
  • According to this [question](http://stackoverflow.com/questions/776787/hibernate-using-jpa-annotated-entities-and-liquibase you do need) you need a hibernate.cfg.xml but you are right that would not be a big problem – willcodejavaforfood Oct 13 '10 at 17:28
  • hey whats the need of migration tool if ,we can create database and stuffs using hibernate-jpa – Ishan Garg Mar 22 '21 at 13:09
  • @IshanGarg Data cannot be migrated via hibernate-jpa. A trivial example was given above: if you add a non-null column to a table, hibernate-jpa migration alone will not know what to to put in that column for existing records. But there are lots of more complex situations too: merging two columns or splitting a column using a particular parsing rule, splitting records in a table into multiple tables etc. For any such situation you need something that hibernate-jpa alone cannot provide. – user625488 Mar 23 '21 at 14:18
2

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"
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • What kind of changes can hibernate.hbm2ddl=auto cope with without screwing up my DB? :) – willcodejavaforfood Oct 13 '10 at 12:55
  • almost any. Except for drop columns, I guess, where you'll have to drop them yourself. – Bozho Oct 13 '10 at 13:08
  • 3
    Do *not* use `hbm2ddl=auto` in a production setting, you are begging for trouble if you do. – matt b Oct 13 '10 at 13:30
  • @matt b - Care to elaborate? :) – willcodejavaforfood Oct 13 '10 at 15:47
  • 1
    First of all, there is no hbm2ddl=auto, only hbm2ddl.auto=update - which you shouldn't use in production – Otto Allmendinger Nov 16 '11 at 09:42
  • 1
    @OttoAllmendinger I'll fix the name of the property. And I know many people say "don't use that in production", but if you are careful and have a staging environment this is a way better option than supporting migration scripts. – Bozho Nov 16 '11 at 09:55
  • for newbs, what file do we place the line `hibernate.hbm2ddl.auto=update`? – Alexander Mills Jan 27 '19 at 08:10
  • you do it in the application.properties file. But please, **DON'T** set that for prod env, or you'll have your DB schema automatically updated based on the Hibernate mapping with your domain objects, which is never, never wise. – kekko12 Aug 06 '19 at 17:11
  • @Bozho You can't version the data itself without migration scripts. Only, most tools to manage migrations - schema and data in one go - are not very good. My go-to solution is to use mybatis migrations for managing migrations (both schema and data), and use hibernate's hbm2ddl to generate entities from the database. The drawback is that you need a database in your build/dev environment. IME, however, that's been the cheapest thing to do, in terms of risks and effort. (This question is old but recurring, leaving this here for reference.) – user625488 Jan 21 '20 at 17:30
  • hey whats the need of migration tool if ,we can create database and stuffs using hibernate-jpa – Ishan Garg Mar 22 '21 at 13:08