Update column and table definitions with Hibernate

1

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 its 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 a SQL script to go through the schemas and that Hibernate identifies the settings that need to be changed, tables and fields that need be created and enforced in the database for each schema.

Thank you in advance.

    
asked by anonymous 06.03.2015 / 18:10

1 answer

3

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

<property name="hibernate.hbm2ddl.auto" value="update" />
  • The "update" value changes the structure of the database to reflect its Java entities.
  • The value "validate" does not change the database, only checks to see if it matches the one 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 existing information.
  • The "create-drop" value recreates the database from scratch, loosing the existing information, and also deletes everything when finished executing.

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

    
06.03.2015 / 19:21