Hibernate/JPA DB Schema Generation Best Practices

  1. It’s always recommended to generate the schema manually, preferably by a tool supporting database schema revisions, such as the great Liquibase. Generating the schema from the entities is great in theory, but were fragile in practice and causes lots of problems in the long run(trust me on this).

  2. In productions it’s always best to have manually generated and review the schema.

  3. You make an update to an entity and create a matching update script(revision) to update your database schema to reflect the entity change. You can create a custom solution(I’ve written a few) or use something more popular like liquibase(it even supports schema changes rollbacks). If you’re using a build tool such as maven or ant – it’s recommend to plug the db schema update util into the build process so that fresh builds stay in sync with the schema.

Although disputable, I’d say that the answer to all 3 questions is: let hibernate automatically generate the tables in the schema.

I haven’t had any problems with that so far. You might need to clean some field up manually from time to time, but this is no headache compared to separately keeping track of DDL scripts – i.e. managing their revisions and synchronizing them with entity changes (and vice-versa)

For deploying on production – an obvious tip – first make sure everything is generated OK on the test environment and then deploy on production.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)