Hibernate/JPA DB Schema Generation Best Practices

前端 未结 5 780
梦毁少年i
梦毁少年i 2020-11-29 01:34

I just wanted to hear the opinion of Hibernate experts about DB schema generation best practices for Hibernate/JPA based projects. Especially:

  1. What strategy

5条回答
  •  有刺的猬
    2020-11-29 02:15

    Manually, because:

    1. Same database may be used by different applications and not all of them would be using hibernate or even java. Database schema should not be dictated by ORM, it should be designed around the data and business requirements.
    2. The datatypes chosen by hibernate might not be best suited for the application.
    3. As mentioned in an earlier comment, changes to the entities would require manual intervention if data loss is not acceptable.
    4. Things such as additional properties (generic term not java properties) on join tables work wonderfully in RDBMS but are somewhat complex and inefficient to use in an ORM. Doing such a mapping from ORM -> RDBMS might create tables that are not efficient. In theory, it is possible to build the exact same join table using hibernate generated code, but it would require some special care while writing the Entities.

    I would use automatic generation for standalone applications or databases that are accessed via the same ORM layer and also if the app needs to be ported to different databases. It would save lot of time in by not requiring one to write and maintain DB vendor specific DDL scripts.

提交回复
热议问题