Creating HSQL to create a table query from a class

I have a very used project that I am currently working on. There are several places where this project can be installed, and in the future it is not known which version is used there and for which version can be updated in the future. Now they are all the same.

My problem is that there can be many changes to the hibernation entity classes, and they can easily be updated to a newer version without any problems, as well as without losing the contents of the database. Just replace the WAR and start, and it should migrate itself.

As far as I know, Hibernate does not change tables if hibernate.hbm2ddl.auto = create, but which actually throws all the data?

So, right now, when the Spring context is fully loaded, it executes a bean that will transfer the database to the current version, switching all the changes from version X to version Y (which version that was previously saved to the database) and manually change the table.

Not too much trouble doing a few hard-coded ALTER TABLEs to add a few columns, but when it comes to adding complete new tables, it’s silly to write all this ...

So my question (s) ::

  • Is there a way to send an entity class and dialect to Hibernate code somewhere and return a valid SQL query to create the table?

  • And even better, somehow create an SQL row to add a column to the table, based on the dialect?

Hope this is not a stupid question, and I haven't missed anything obvious when it comes to Hibernate ...

+5
4

, . Hibernate hbm2ddl ( ant maven) DDL , - , "diff", . , , , , ..

, , liquibase, .

+2

hibernate.hbm2ddl.auto=update

, .

+4

Do not use Hibernate ddl. It deletes your data if you want to perform a migration. I suggest you take a look at Liquibase. Liquibase is database version control. It works through change sets. Each change set can be created manually or you can let Liquibase read the Hibernate configuration and create a change set.

Liquibase can be started using Spring to fit your project; -)

+2
source

All Articles