Hibernate: DDL & # 8596; Classes in which direction?

What do you think is the β€œright” way to use Hibernate? To design an object level model (classes) and then generate DDL? To design a model at the relational level (tables) and then generate classes from it?

What are the advantages and disadvantages of each method?

In the case of creating DDL, you first generate classes from it, and then you can add some code to the classes, which happens if you want to add a new column to the table, for example, you need to change the class accordingly. Manually? Does Hibernate provide a tool to change a class without fully restoring the class (deleting user code)?

thanks

+4
source share
2 answers

I prefer the following approach:

  • In the early stages of the project, you develop classes and generate DDL from them so that you can easily change your domain model as often as you need.

    However, when developing a domain model, you still need to save the database schema in order to avoid creating a model that would lead to inefficient access to the database.

  • As the project evolves, and the domain model becomes more stable, you can start supporting the database schema as a separate artifact. This makes it easy to fine-tune indexes and constraints.

    At this point, you need to manually synchronize the schema with the domain model, but you still need it because the automatic DDL updates provided by Hibernate are not reliable enough for production use, so you need to track changes and record migration scenarios for them.

+4
source

I would first generate the domain classes, and then use Hibernate to create the DDL.

This is because it allows you to define associations between tables (ManyToOne, etc.), which will lead to the creation of corresponding foreign keys. You can also use annotations to specify unique indexes to be displayed in the generated DDL.

However, once your project is stable, you should not use hibernation to update your schema after changes to your domain class.

+2
source

Source: https://habr.com/ru/post/1386341/


All Articles