Update database schema with hibernate
Asked Answered
A

5

12
<property name="hibernate.hbm2ddl.auto">update</property>

i can create my database schema, it automatically add properties, constraint, key etc... But what about UPDATE the database schema? If i remove some property from my entities, hibernate doesn't remove it, or if i change some constraint, hibernate doesn't touch constraint already created...

So, there is a way to make hibernate really update the database schema?

Thanks.

Adenovirus answered 1/1, 2011 at 16:52 Comment(0)
W
8

We created a tool for our own that creates the necessary drops of database columns and tables and add these drops to the SQL generated for database updates. But we had to add some extras to the SchemaUpdate generation to make it work:

  • We had to add checks for not-null properties. This includes issuing an UPDATE statement on the data to eleminate nulls where possible, leading to the next point of default values.
  • We had to add checks for default values of columns. The defaults are inferred by the nullability of a column and its datatype. Primitives are always initialized to zero or false, not-null enums to its first enum value, but for other objects the script has to be modified manually.
  • We even added support for resizing varchar columns, because there were cases where the database column length and the @Column(length) differed.

But to put it all together, a complete tool cannot be created this way, because what if a column is renamed in code? What if the type changes in a way not automatically convertible (bool to date?). If you don't have access to a refactoring history, you cannot always propagate changes.

Whorish answered 9/1, 2011 at 15:24 Comment(0)
E
5

No there is not. hbm2ddl is not meant to do a complete management of your schema migrations. It's best if you use it only for additive changes to your schema and hand edit (the generated scripts) for anything else.

Embody answered 1/1, 2011 at 23:47 Comment(0)
S
5

We currently use liquibase to do automatic database changes in a database agnostic way. It may be possible to extract liquibase commands directly from your hibernate annotations, but I don't think such a tool exists so you'll probably have to do it yourself.

Schmaltz answered 6/1, 2011 at 15:6 Comment(2)
What do you mean saying liquibase is database agnostic? I tried to run a liquibase changeset generated on one RDBMS on another RDBMS (i.e. database program) and it didn't work.Aitch
Fletch - I've used it for years and it is db agnostic but if you hardcode your <sql> to a specific db then it won't be. Try to use the xml nodes like <insert> <update>... then it will translate for you.Fyn
C
2

Some projects that might be useful to manage schema changes:

  • mybatis (1049 questions tagged on SOW)
  • Liquibase (663 questions tagged on SOW)
  • Flyway (400 questions tagged on SOW)

Another resource that you can find useful is the feature comparison in the Flyway website (There are other related projects mentioned there).

Charlottecharlottenburg answered 20/1, 2015 at 21:20 Comment(0)
T
0

Hibernate provides a class called SchemaUpdate that is able to synchronise a set of hibernate mappings with a database schema

Old post, but let the community know if it is good :)

Teniafuge answered 6/1, 2011 at 14:45 Comment(1)
This link seems to be broken, at least it doesn't show any contents for me, only the article's title.Sew

© 2022 - 2024 — McMap. All rights reserved.