How does Hibernate.hbm2ddl.auto work?

Asked

Viewed 18,970 times

5

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

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

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

2 answers

11


The options are these:

  • validate: validate the schema, make no changes to the database.
  • update: update the schema.
  • create: creates the schema, destroying previous data.
  • create-drop: drop the schema when the session ends.

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

Reply link on Soen link

5

  • How it works?

Answered by our friend Rafael.

  • When should I use?

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

Is it a good idea to let it happen automatically, creating, updating or removing any entity by Hibernate?

No, as much as the whole 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 safer way for development. Never use this in production, it can be a fatal mistake.

But we can talk in context, if your context is to do your own study and test project, maybe, but I don’t think it’s good practice. In my experience with tools that often do something related to development automatically, tend to fail miserably.

  • is a good practice?

No. I see it as 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 full control of the product, from the way it is delivered, to its architecture and code quality. Think how simple it is for Hibernate to replicate a wrong line of code for the database and thus cause chaos and this was totally out of your reach.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.