Update column and table settings with Hibernate

Asked

Viewed 1,372 times

2

Good afternoon.

I would like to know and confirm if you have any way to update table and column definitions in each schema using Hibernate.

For now, what I’ve seen so far is that Hibernate only creates tables and columns, but does not delete or modify their settings, such as the size of a field.

I have already implemented Flyway in the application and it is working correctly, but the idea is that this is done without creating an SQL script to go through the schemas and that Hibernate identifies the definitions that need to be changed, tables and fields that need to be created and that apply in the database for each schema.

Thank you in advance.

  • I understand this Hibernate feature to create or update database schema more as a resource time design (where if something goes wrong or at each test you simply recreate the base from scratch) than to upgrade bases in production or even UAT. I don’t think it’s the function of the application to update your database because unfortunately this is not trivial or risk-free. Updating the database should be one of the application version update tasks - it can and should be automated, but it should be done by a tool specialized in this (not the case of Hibernate).

1 answer

4


Within the persistence.xml (or hibernate.cfg.xml or some other equivalent mechanism) you can define the following property:

<property name="hibernate.hbm2ddl.auto" value="update" />
  • The "update" value changes the database structure to reflect its Java entities.
  • The "validate" value does not change the database, it only checks if it matches that specified in the entities. If it does not match, an exception will be thrown at startup.
  • The "create" value recreates the database from scratch, losing the information there.
  • The "create-drop" value recreates the database from scratch, losing the existing information there and also deletes everything after running.

So it seems to me that what interests you is the "update". But I must warn you that it is not advisable to use this in production because of the inherent risks of making structural modifications in tables without the accompanying and detailed manual planning of this. In production environment it is recommended to use only "validate".

  • Thank you very much Victor, about the property Hibernate.hbm2ddl.auto already knew, but I wonder if you have a way, to use this for example, and besides only create tables and fields can also change the field definitions, like, for example, the maximum size of the value of a column in a record. Is it possible? I did tests here and this property just creates, I also saw on Stackoverflow in English about it. That’s the main question.

  • 1

    @Giancarlogiulian Looking quickly, I found someone saying that the update is not well supported and the Hibernate staff confirming: https://forum.hibernate.org/viewtopic.php?p=2209886&sid=a3414397090d0ffd1338c19030bd0972#p2209886 - However, that was in 2003 and it is very likely that the situation today will be quite different. I also found this question abandoned in Soen: http://stackoverflow.com/questions/23494157/what-is-exact-behavior-of-hibernate-hbm2ddl-auto-update-in-hibernate

  • Thank you very much for the clarification, I will now see the links you gave me.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.