How hibernate.hbm2ddl.auto works?

4

What values can I use in this property? ex: Update

<prop key="hibernate.hbm2ddl.auto">update</prop>

How does it work? When should I use it? is it a good practice?

    
asked by anonymous 05.10.2015 / 17:15

2 answers

7

The options are as follows:

  • validate : validate the schema, does not make any changes to the database.
  • update : update the schema.
  • create : creates schema, destroying previous data.
  • create-drop : drop the schema when logging out.

Then you have to evaluate what is best for your project, usually I use update .

Response link in SOen link

    
05.10.2015 / 17:26
3
  • How does it work?

Answered by our friend Rafael .

  • When should I use it?

This is a little ambiguous, as it can trigger good discussions, but it is good to think of it as giving responsibility to a technology that will theoretically take care of all the development actions in the database.

Is it a good idea to let Hibernate automatically create, update, or remove an entity?

No, even though the entire development for the database forms the same codebase, it does not mean that the automation of one over the other will work. Validating your scripts manually is always a more secure way to develop. Never use this in production, it can be a fatal error.

But we can talk in context, if its context is to do a project of study and test, maybe, but I do not believe it is a good practice. In my experience with tools that typically do something related to development automatically, they tend to fail miserably.

  • Is it good practice?

No. I see it as a bad practice, both on the developer side and on the client side that will use the product. Giving development responsibilities to any tool is not good practice. Good practice is to have total control of the product, from the way it is delivered, to its architecture and code quality. Think of how simple it is for hibernate to replicate a wrong line of code to the database and thus cause chaos and this has been totally out of your reach.

But as I said, we can generate good discussions, because if we focus on good practices we would not use ORM, as it is certainly an anti-pattern for Object Orientation.

    
06.10.2015 / 00:10